Mark Kelly Mark Kelly - 3 months ago 18
Python Question

Processing form data with a Python CGI Script

Can anyone point out where I am going wrong here. I have two scripts, one for the form and the other for the processing. It looks correct but after two hours of staring at it I cannot see where I am going wrong.
Here are the two scripts, they are very short so please take a look at it.

The Form:

#!/usr/bin/python
import os
import cgi
import cgitb



print("Content-Type: text/html\n\n")
print("")

print'''<html>
<head>
<meta charset="utf-8">
<title>Marks Sonitus Practice</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Practice">
<meta name="author" content="CGI Practice">
</head>
<body>
<form action="process_data.py" method="post">
<html><span> First &nbsp;&nbsp;&nbsp;</span></label>
<input type="text" name="firstname"/>
<input type="submit" name ="submitname" value="Submit Name"></form>
</body>
</html>'''


The script to process the form:

#!/usr/bin/python
import os
import cgi
import cgitb
cgitb.enable(display=0,logdir="/var/www/cgi-bin/error-logs")

file_name = "/var/www/cgi-bin/practice/process_practice.py"
f = os.path.abspath(os.path.join(file_name))
try:
open(f)
except:
print"This file could not be found!"
form = cgi.FieldStorage(f)
firstname = form.getvalue('firstname')
print firstname


Can anyone show me where I am going wrong with this?

Answer

Well thats simple in your form replace the action=process_data.pyto action=http://localhost/cgi-bin/practice/process_practice.py or may be action=http://localhost/cgi-bin/practice/process_data.py whatever the name of that script is.