Read the file with python cgi - python

I have data with a .txt extension . I want to read the file, but the result of his error result < cStringIO.StringO object at 0x7f5078d18068 > What 's wrong ?
My file
Abedus
Accer
Chrome
HTML
<html>
<body>
<form enctype="multipart/form-data" action="http://localhost/cgi-bin/my.py" method="post">
<p>File: <input type="file" name="filename"></p>
<p><input type="submit" value="Upload"></p>
</form>
</body>
</html>
CGI Python
#!/usr/bin/python
import cgi, os
import cgitb; cgitb.enable()
form = cgi.FieldStorage()
data = form['filename'].file
print "Content-Type: text/plain"
print ""
print "result %s" % data,

Related

Python and cgi output in the same html page

From inside a web page I define short python code in a texarea field which should be run via cgi. I have no problem to create a new web page with the output of the external python run. What I like is instead the output below the textarea field in an iframe or whatever. But not as a new html page.
I have a html page:
<!DOCTYPE html>
<html lang="en-US">
<body>
<style>
textarea {white-space: pre-wrap; overflow-wrap: break-word;}
</style>
<form action="/cgi-bin/getword3.py" method="post" >
<textarea name="py-code" cols="40" rows="4"></textarea></br>
<input type="submit" value="Submit" />
</form>
*** the output of the cgi should appear here ***
</body>
</html>
my not working python cgi is:
#!/usr/bin/env python3
import cgi, cgitb
cgitb.enable()
from subprocess import Popen, PIPE
form = cgi.FieldStorage()
if form.getlist('py-code'):
text_content = form.getvalue('py-code')
else:
text_content = "Not entered"
process = Popen(["python3", "-c", text_content], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
return stdout.decode())

HTML input type as email & password

I am writing a basic html program to give input as email and password. Below is the html program
<html>
<body>
<form action='show_commands_html.py' method='get'>
<label for="myname">Enter Your Name</label>
<input id="myname" type="email" name="firstname" value="test#in.com" />
<label for="mypass">Enter Your password</label>
<input id="mypass" type="password" name="Password" value="test121$" />
<input type="submit">
</form>
</body>
</html>
respective python code
import os
print "Content-type: text/html\n"
qs = os.environ['QUERY_STRING']
if 'firstname' in qs:
name = qs.split('&')[0]
name = name.split('=')[1]
if 'Password' in qs:
passw = qs.split('=')[2]
username = name
password = passw
print "<html>"
print "<body>"
print "<h1>%s</h1>" %username
print "</pre>"
print "</body>"
print "</html>"
print "<html>"
print "<body>"
print "<h1>%s</h1>" %password
print "</pre>"
print "</body>"
print "</html>"
I am getting below answers from web page
test%40in.com
test121%24
Instead of
test#in.com
test121$
Any help please ?
Remember to add UTF-8 inside <head>.
Method 1.
You can add new line inside your .py code,
"Content-Type: text/html; charset=UTF-8\r\n";
Method 2. Add new string and test if it works.
u = 'idzie wąż wąską dróżką'
uu = u.decode('utf8')
s = uu.encode('cp1250')
print(s)
Method 3. Add these 2 lines above your code.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
Tip
Make sure, you changed in your code editor:
Settings -> File Encoding -> Switch to: Unicode (UTF-8)

Pass string from html to python script

I need help, I have a Python script that do some work and print html web page.
I need to pass string from that outputed web page to that script.
Is that possible ?
In future I want to use radio buttons where user will thick data for plotting from predefinned ones.
Thanks for any advice...
My code:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
import subprocess
import os
print "Content-type: text/html\n\n";
web = """
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
</head>
<body>
<div id='wrapper'>
<div id='header'>
</div>
<div id='content'>
<div class='content'>
<div><h3>GRAPH</h3></div>
<br>
<div style='height:520px;width:1000px;overflow:scroll;overflow-x:auto;overflow-y:hidden;'>
<img src='http://localhost/steps.png' alt='image' />
</div>
<br>
<button onclick="myFunction()">REFRESH</button>
<script>
function myFunction() {
location.reload();
}
</script>
<br>
<form action='/cgi-bin/test.py' method='post'>
nazov suboru: <input type='text' data='data'> <br />
<input type='submit' value='Submit' />
</form>
</div>
</div>
</div>
</body>
</html>
"""
print web
proc = subprocess.Popen(['gnuplot','-p'],
shell=True,
stdin=subprocess.PIPE,
)
proc.communicate("""
reset
set terminal pngcairo enhanced size 3000,500 font 'Arial,11' rounded;
set output '/opt/lampp/htdocs/%s.png'
set datafile separator "|"
plot \
\
'%s.txt' u 0:3 sm cs w l ls 1 t 'X-suradnice',\
'%s.txt' u 0:4 sm cs w l ls 2 t 'Y-suradnice',\
'%s.txt' u 0:5 sm cs w l ls 3 t 'Z-suradnice'
"""%(data,data,data,data))
Your question is quite hard to understand, but I think you are trying to work out how to access the data parameter submitted by the form POST. You can do that with the cgi module:
import cgi
form = cgi.FieldStorage()
data = form.getvalue('data')

FieldStorage is empty despite a GET request with fields

I am beginning with python CGI programming, using mod_python in apache2, and am trying to retrieve the GET fields in HTTP requests to a simple .py page.
The code:
#!/usr/bin/python
import cgi
import cgitb; cgitb.enable()
print 'Content-type: text/html\n'
print '''
<html>
<body>
'''
form = cgi.FieldStorage()
l = len(form.keys())
print "<p>%s field(s) set.</p>" % l
print '''
</body>
</html>
'''
The page prints "0 field(s) set." What could be wrong here? So far in my search for an answer I haven't find a parameter in mod_python of apache2 that would block the transmission of GET fields to the CGI script.
cgi.FieldStorage relies on a multipart form being POSTed, not sent via GET. Change the method on your form and make sure you have multipart in the form tags:
<form action="" method="post" multipart>
<input type="file" name="file">
<input type="submit" name="submit" value="Submit">
</form>
When you use the GET method, cgi.FieldStorage just grabs the query string, which will at best give you the name of the file you're trying to submit.

Upload files without FieldStorage

How could i upload a file to a server without using FieldStorage in python?
Here is a toy program snippet that should help get you started. Try reading RFC 1867 as well for more guidance.
#!/usr/bin/python
import os
import sys
buf = sys.stdin.read(512)
print "Content-type: text/html\n\n";
print '<html>'
print '''
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="f">
<input type="submit">
</form>
'''
print buf
print '</html>'
You can use os.environ.items() to get a list of environment variables, notably CONTENT_LENGTH and CONTENT_TYPE (specifically the boundary key/pair) so you know where the demarcation points are for the uploaded content.

Categories