问题
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 another PHP page, response.php that will display information based on my processed data and that has an
<?php include '/connect_database.php';?>
line.
Up till now, I am able to get the POST info, process it and send it through a JSON to connect.php, but I am not able to make find_match.py redirect to response.php. My code looks like this:
In pyscripts/find_match.py:
print "Content-type: text/html"
print
print "<html><head>"
print "</head><body>"
import cgi
import cgitb
cgitb.enable()
try:
form = cgi.FieldStorage()
fn = form.getvalue('picture_name')
cat_id = form.getvalue('selected')
except KeyError:
print 'error'
else:
# code to process data here
data_to_be_displayed = # data to be used in connect.php; it's an array of ids
import httplib, json, urllib2
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
conn = httplib.HTTPConnection('192.168.56.101:80')
#converting list to a json stream
data_to_be_displayed = json.dumps(data_to_be_displayed, ensure_ascii = 'False')
conn.request("POST", "/connect_database.php", data_to_be_displayed, headers)
response = conn.getresponse()
text = response.read()
# print response.status, text
conn.close()
# WHAT I WANT TOT DO HERE
if response.status == 200:
redirect('/response.php')
print "</body></html>"
In response.php:
<!DOCTYPE html>
<html>
<head>
<title>Response Page</title>
</head>
<body>
<div id="main">
<?php include '/connect_database.php';?>
</div>
</body>
</html>
I have found some info about the urllib.HTTPRequestHandler class and the Location header, but I don't know how to use them. Tried using
<meta http-equiv="refresh" content="0;url=%s" />
in the HEAD tag, but it doesn't work. Please help.
回答1:
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"
回答2:
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.
来源:https://stackoverflow.com/questions/25161149/python-cgi-how-to-redirect-to-another-page-after-processing-post-data