Analysis in python using form [duplicate] - python

I have created html form with text box and button
enter ur search keyword
My requirement is to pass the text box value in to my test.py code, is there any way to do it.
Please suggest me how do it.

in the form action form action="", put the location of your cgi script and the value of the textbox will be passed to the cgi script.
eg.
<form name="search" action="/cgi-bin/test.py" method="get">
Search: <input type="text" name="searchbox">
<input type="submit" value="Submit">
</form>
in your test.py
import cgi
form = cgi.FieldStorage()
searchterm = form.getvalue('searchbox')
thus you will get the key word entered in search text box in searchterm variable in python.

I had the problem with getting the code in browser. Installed python and xampp.
I put the .py script in cgi-bin and I called it from an html page like that
<form name="input" action="../cgi-bin/name_of_script.py" method="get">
Put the html page in htdocs of xampp.
In the httd.conf find the line
AddHandler cgi-script .cgi .pl .asp
and change it to
AddHandler cgi-script .cgi .pl .asp .py
In the script add the version of the python you have
for example I added
#!C:\Python27\python.exe
because I have installed python27 in the above directory.
Also if you print something in python put this line on top of the script
print "Content-Type: text/html; charset=utf-8\n\n";
The above script of the searchbox it worked fine for me.
My operating system is Windows really torchered me, I searched and searched for the above solution.

Related

Flask vs Web.py: Served files processed differently

A .tar.bz2 file served from web.py is saved as .tar.bz2 from the browser. However when served by flask, the .bz2 extension is removed (but running file against the file still identifies it as a .bz2 file).
I have both frameworks running (web.py on python 2.7 and flask on python 3.7).
I manually put the same .tar.bz2 file in a directory and serve it from the web page.
Web.py:
=======
<form method="get" action="$fName">
<div id="formsubmitbutton">
<input type="submit" name="Retrieve File" value="Retrieve File" class="button3" />
</div>
</form>
Flask:
======
<form method="get" action="{{ fName }}">
<div id="formsubmitbutton">
<input type="submit" name="Retrieve File" value="Retrieve File" class="button3" />
</div>
</form>
I would expect both setups to return fName.tar.bz2. Chrome is returning the following and I have no clue why, but suspect it has something to do with my problem:
"Resource interpreted as Document but transferred with MIME type application/x-tar"
UPDATE
Thanks for the tip. From Chrome though, that was the entire error message, minus the actual file name since there is customer information in there. Here is is edited error:
Resource interpreted as Document but transferred with MIME type application/x-tar: "http://foo.example.com:5000/static/1564445156.7369618/filename.tar.bz2?Retrieve+SSD=Retrieve+SSD".
Also, there is no traceback from web.py or Flask, because there is no error. it's just that when you click on retrieve file, web.py returns the full filename with .bz2 extenstion, and Flask returns the file without the .bz2 extension.
The .bz2 files I am trying to serve are in the /static directory. By adding an app.route('/static') function, I was able to serve the file as a .bz2 without it being stripped of the .bz2 suffix:
#main.route('/static')
def sendfile():
for key,val in request.values.items():
filedir = '/home/admin/Flask/httsTools'+os.path.dirname(key)
filename = os.path.basename(key)
return send_from_directory(filedir, filename, as_attachment=True)
Whether or not this is the recommended way to do it I'm not sure, but it works.
Thank you for your help.

How to execute external commands with Python using Apache2 and CGI

I have an Apache web server. I have an html file and a Python script located at var/www/myfolder. I am submitting a form through the html file which is handled by my Python script (when the submit button is clicked). But once handling the form, my Python script executes an external command. (see the following code)
webpage.html
<!DOCTYPE html>
<html>
<head>
<title>Webform</title>
</head>
<body>
<h3>Webform</h3>
<form action="myscript.py" method="POST">
<p>Name: <input type="text" name="name"></p>
<input type="submit" value="Submit">
</form>
</body>
</html>
myscript.py
#!/usr/bin/python -W
import cgi, cgitb
import sys
import os
# Get data from fields
form = cgi.FieldStorage()
name = form.getvalue('name')
print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2></br></br>'
print '<p>Name: %s</p>' % (name)
print '</body>'
print '</html>'
os.system("./other_script.py") # gives me an error with permission
On my server I am user007 but I know that when I click submit on the html file, the Python script is executed as apache2 user. The problem is I don't know the password for this user (can't us sudo). Are the two ideas possible:
1) change from apache2 to user007 when trying to execute other_script.py from myscript.py
2) I know that you can change users using suEXEC but I have no idea how to use it.
Any suggestions?
I should let you know that locally both the python scripts are executing fine.
Edit 1:
I get this message before the error occurs: WARNING: HOME is not set, using root: /

Handle HTML Form Data with Python?

I'm trying to use Python and HTML together. What I'm trying to do is create an HTML Form that will submit data to a python file and that python file will then handle the data. But I'm not getting it to work.
Here is my python code:
form = cgi.FieldStorage() # instantiate only once!
name = form['Sample Name'].value
and this is my HTML code:
<form method='POST' action='/functionGen.py'>
Name: <input type='text' name='Sample Name'>
<input type='submit' value='Begin Storing'>
</form>
What ends up happening is I just see my python code in the browser, the file doesn't begin handling the data.
What can I do?
You should know, that you are getting plain python source document via http protocol now. If you want to use CGI mechanism, you should place youre .py in cgi-enabled directory. It means that you need http server.
related questions
How to run Python CGI script
How do I set up a Python CGI server?

How can i do python form post request?

form.html:
<form action="p.py" method="post">
<input type="text" id="id_text" name="name_text" />
<input type="submit" id="id_submit" name="name_submit" />
</form>
p.py:
#!/usr/bin/python
#what to write here...
Both files are put in /var/www/ , now from http://example.com/form.html, if I click the submit button would I execute the p.py and how can I get the value of textbox?
It's apache/httpd webserver installed on computer.
1st, to make p.py run by apache, you need to check:
1, config apache site for a python script, you may choose: cgi/fastcgi
such as: ScriptAlias /cgi-bin/ /usr/local/xxxxxx/cgi-bin/
2, make sure your p.py file is runable, modify by chown/chmod
2nd, to get the value from a form, you need to understand cgi knowleges and get data from environment,
or much easier by using cgi module:
import cgi
form = cgi.FieldStorage()
v = form.getvalue('id_text','')
print """Content-type: text/html
<html>
<body> submit value:%s </body>
</html>""" % (v)
at last, I suggest:
put runable scripts to a separated dir, away from /var/www/
using a web framework for web develop. It's too deep to use cgi level technology in 2012.

Problem with python cgi in Firefox

I have written an html form and am trying to incorporate a python cgi script with it. I have already configured my apache server to execute cgi scripts from the cgi-bin directory. Here's the html form:
<html>
<body>
<form name="input" action="c:/xampp/cgi-bin/test2.py" method="post">
<input type="text" name="qry" />
<input type="submit" value="GO!" />
</form>
</body>
</html>
And here's the test2.py cgi script:
#!c:/Python27/python.exe -u
import cgi
import cgitb
cgitb.enable()
form = cgi.FieldStorage()
qry = form["qry"].value
print "Content-Type: text/html"
print
print "<html"
print "<body>"
print qry
print "</body>"
print "</html>"
The html page is located in my htdocs folder and the cgi script is in the cgi-bin directory. However, when I enter something into the form and submit, firefox returns an error message saying: "Firefox doesn't know how to open this address, because the protocol (c) isn't associated with any program". Why is this error occurring? Does it have something to do with my path to the cgi script in my html page? Thanks in advance!
You are correct: it has something to do with the path to the CGI script on the HTML page. The form's action attribute should refer to the path where the CGI script is getting interpreted on the server, e.g., /cgi-bin/test2.py.
Since you made this mistake, I'm assuming you are new to web development. Consider using mod_wsgi and a framework like Django instead of CGI, especially if you expect a lot of traffic or you are making a web application and not just handling a single form.

Categories

Resources