I\'m trying to write a Python script, in pyscripts/find_match.py that will process data received from a POST in an upload.php page, send it to connect.php and then redirect to a
I wish people didn't keep trying to write CGI in 2014.
To redirect in a plain CGI application, you just need to output the destination next to a "Location:" header. However, you have already closed the headers and printed a blank HTML document at the top of your script. Don't do that: it's not only wrong for the redirection, it's also wrong for your alternative path, the form error, since you have already closed the HTML tags.
Instead, start your script like this:
# No printing at the start!
import cgi
...
try:
form = cgi.FieldStorage()
fn = form.getvalue('picture_name')
cat_id = form.getvalue('selected')
except KeyError:
print "Content-type: text/html"
print
print "<html><body>error</body></html>"
else:
...
if response.status == 200:
print "Location: response.php"
1) Where is the shebang? (#!/usr/bin/env python)
2) print 'error' is a problem. cgi scripts stdout is the browser, the word 'error' will be problematic.
3) chmod 755 the script
I throw more redirects than webpages, here is the function I use.
def togo(location):
print "HTTP/1.1 302 Found"
print "Location: ",location,"\r\n"
print "Connection: close \r\n"
print ""
I don't know if that last print statement is needed, and the Connection close header seems to be optional to most clients, but I leave it because it is supposed to be in there, I think. RFC reading tends to put me right to sleep.
When I first write cgi scripts, I don't use cgi.FieldStorage, I hardcode the values so I can test it on the command line, after I get that working, I try it in a browser with the hard coded values, when that's working I add in the cgi.FieldStorage.
Check out
import cgitb
cgitb.enable()
I know it can be aggravating, I've been there. Good luck.