【发布时间】:2014-09-30 15:05:02
【问题描述】:
我有一个 upload.php 页面,它通过表单将一些数据发送到 Python CGI 脚本,然后我在后台处理数据,我想重定向到另一个 php 页面,response_page.php,它显示基于信息处理后的数据。但是,我无法将数据发送回 PHP 并同时进行重定向。
我的代码:
#!/usr/bin/env python
import cgi
import cgitb
cgitb.enable()
try:
form = cgi.FieldStorage()
fn = form.getvalue('picture_name')
cat_id = form.getvalue('selected')
except KeyError:
print "Content-type: text/html"
print
print "<html><head>"
print "</head><body>error</body></html>"
else:
...
# here I processed the form data and stored it in data_to_be_displayed
# data to be processed and displayed in the response page
data_to_be_displayed = [1,2,3]
import httplib, json, urllib
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')
postData = urllib.urlencode({'matches':data_to_be_displayed})
conn.request("POST", "/response_page.php", postData, headers)
response = conn.getresponse()
if response.status == 200:
print "Location: /response_page.php"
print # to end the CGI response headers.
conn.close()
我发现了这个:How to make python urllib2 follow redirect and keep post method,但我不明白我应该如何在我的代码中使用 urllib2.HTTPRedirectHandlerClass。
【问题讨论】:
标签: python post cgi urllib2 url-redirection