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())
Related
I want to run a script when the button with the bottle is pressed. But I get 404 errors every time. It says localhost: //File.py in the address bar, but I don't know how to route it.
app.py
from bottle import *
#route('/')
def home():
return template('deneme.html')
run(host='localhost',port=8080)
File.py
#!/usr/bin/python
import cgi, cgitb
form = cgi.FieldStorage
username = form["username"].value
emailaddress = form["emailaddress"].value
print("Content-type: text/html\r\n\r\n")
print( "<html>")
print("<head>")
print("<title>First Script</tittle>")
print("</head")
print("<body>")
print("<h3>This is HTML's Body Section</h3>")
print(username)
print(emailaddress)
print("</body>")
print("</html>")
deneme.html
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="File.py" method="post">
username: <input type="text" name="username"/>
<br />
Email Adress: <input type="email" name="emailaddress"/>
<input type="submit" name="Submit">
</form>
</body>
</html>
You shouldn't use cgi and cgitb with Bottle, Flask, or any other Python web framework.
Try something like
from bottle import run, route, request
#route('/')
def home():
return template('deneme.html')
#route('/foo')
def foo():
return '%s %s' % (request.forms.username, request.forms.email)
run(host='localhost',port=8080)
(and change the action of your form to action="/foo").
Also, consider using Flask; it's in the same vein as Bottle, but more popular and more maintained.
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,
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')
I have been following along in Programming Python 4th Edition. One of the tasks is to write a web page that uses cgi to call a python script. It all seems simple enough, but rather than run the script, the browser echos the script instead. I think it may be related to the shebang but I am not sure.
The HTML:
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8" />
<title>Interactive Page</title>
</head>
<body>
<form method=POST action="cgi-bin/cgi101.py">
<p><b>Enter your name</b></p>
<p><input type=text name=user /></p>
<p><input type=submit title=Submit /></p>
</form>
</body>
</html>
The script:
#!python
import cgi
form = cgi.FieldStorage()
print('Content-type: text/html\n')
print('<title>Reply Page</title>')
if not 'user' in form:
print('<h1>Who are you?</h1>')
else:
print('<h1>Hello <i>%s</i></h1>' % cgi.escape(form['user'].value))
Instead of a new page showing "Hello Tim" I'm getting a new page with the script dumped into it.
This is an Apache configuration issue that I have not quite worked through. Now when I run it, I get a file not found error from Apache. Progress I guess...
Just doing a basic python project with HTML file, i came across a tutorial which gave an idea about how i can execute the code,
here is the HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>Admin Login</title>
</head>
<body>
<big><big>Login
Here<br>
<br>
</big></big>
<form action="/var/www/cgi-bin/Login.py" name="LoginForm"><big>Administration
Login<br>
User Name<br>
<input name="UserName"><br>
<br>
<br>
Password<br>
<input name="PassWord"><br>
</big><br>
<br>
<br>
<input type="submit">
<br>
</form>
__ <br>
</body>
</html>
and the python code..
#!/usr/bin/python
import cgi
import cgitb; cgitb.enable()
# get the info from the html form
form = cgi.FieldStorage()
#set up the html stuff
reshtml = """Content-Type: text/html\n
<html>
<head><title>Security Precaution</title></head>
<body>
"""
print reshtml
User = form['UserName'].value
Pass = form['PassWord'].value
if User == 'Myusername' and Pass == 'MyPasword':
print '<big><big>Welcome'
print 'Hello</big></big><br>'
print '<br>'
else:
print 'Sorry, incorrect user name or password'
print '</body>'
print '</html>'
The problem is, when i submit the username and password, it just shows the whole code back on the browser and not the required Welcome message :(. I use Fedora13 .. can anyone tell me what is going wrong? I even changed the permissions of the file(s).
Most likely, your webserver is not configured to execute the script. Even if it's marked as 'executable' in the file system, that doesn't necessarily mean the webserver knows that it should be executing .py files (rather than just serving them 'straight up'). Have a look here if you're running Apache: http://httpd.apache.org/docs/2.0/howto/cgi.html
<form action="/var/www/cgi-bin/Login.py" name="LoginForm">
Try
<form action="/cgi-bin/Login.py" name="LoginForm">
/var/www is probably the path from your ftp site. The web server will only look inside /var/www, so it puts that part there automatically.