Python CGI:处理POST数据后如何重定向到另一个页面 [英] Python CGI: How to redirect to another page after processing POST data
问题描述
我正在尝试在pyscripts/find_match.py中编写一个Python脚本,该脚本将处理从upload.php页面中的POST接收的数据,将其发送到connect.php,然后重定向到另一个PHP页面,以响应. php会根据我处理过的数据显示信息,并且具有
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';?>
行.
到目前为止,我已经能够获取POST信息,对其进行处理并通过JSON将其发送到connect.php,但是我无法将find_match.py重定向到response.php.我的代码如下:
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:
在pyscripts/find_match.py中:
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>"
response.php:
In response.php:
<!DOCTYPE html>
<html>
<head>
<title>Response Page</title>
</head>
<body>
<div id="main">
<?php include '/connect_database.php';?>
</div>
</body>
</html>
我找到了有关urllib.HTTPRequestHandler类和Location标头的一些信息,但是我不知道如何使用它们. 尝试使用
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" />
HEAD标记中的
,但它不起作用. 请帮忙.
in the HEAD tag, but it doesn't work. Please help.
推荐答案
我希望人们在2014年不要继续尝试编写CGI.
I wish people didn't keep trying to write CGI in 2014.
要在普通的CGI应用程序中重定向,只需要在"Location:"标头旁边输出目标即可.但是,您已经关闭了标题,并在脚本顶部打印了空白的HTML文档.不要这样做:重定向不仅是错误的,而且您的替代路径(格式错误)也是错误的,因为您已经关闭了HTML标签.
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"
这篇关于Python CGI:处理POST数据后如何重定向到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!