【发布时间】:2014-09-29 10:24:28
【问题描述】:
我正在尝试在 pyscripts/find_match.py 中编写一个 Python 脚本,它将处理从 upload.php 页面中的 POST 接收到的数据,将其发送到 connect.php,然后重定向到另一个 PHP 页面,响应。 php,它将根据我处理的数据显示信息,并且具有
<?php include '/connect_database.php';?>
线。
到目前为止,我能够获取 POST 信息,对其进行处理并通过 JSON 发送到 connect.php,但我无法将 find_match.py 重定向到 response.php。我的代码如下所示:
在 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 中:
<!DOCTYPE html>
<html>
<head>
<title>Response Page</title>
</head>
<body>
<div id="main">
<?php include '/connect_database.php';?>
</div>
</body>
</html>
我找到了一些关于 urllib.HTTPRequestHandler 类和 Location 标头的信息,但我不知道如何使用它们。 尝试使用
<meta http-equiv="refresh" content="0;url=%s" />
在 HEAD 标记中,但它不起作用。 请帮忙。
【问题讨论】:
标签: python redirect cgi url-redirection http-redirect