【问题标题】:Python CGI: How to redirect to another page after processing POST dataPython CGI:如何在处理 POST 数据后重定向到另一个页面
【发布时间】: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


    【解决方案1】:

    我希望人们不要在 2014 年继续尝试编写 CGI。

    要在纯 CGI 应用程序中重定向,您只需在“Location:”标头旁边输出目标。但是,您已经关闭了标题并在脚本顶部打印了一个空白 HTML 文档。不要那样做:不仅重定向是错误的,你的替代路径也是错误的,表单错误,因为你已经关闭了 HTML 标记。

    相反,像这样开始你的脚本:

    # 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"
    

    【讨论】:

    • 它不起作用。 response.php 页面位于 pyscripts 文件夹之外,因此我也尝试使用“Location: ../response.php”。我还需要在php页面中写一些东西吗?无论哪种方式,重定向都不会在地址栏中视觉上发生。这是正常行为吗?
    • 没有。您在页面本身中得到了什么(如果您查看视图源)?
    • 500 内部服务器错误

      Internal Server Error

      服务器遇到内部错误或配置错误,无法完成您的请求。

      请通过[no address given]联系服务器管理员到告知他们此错误发生的时间,以及您在此错误之前执行的操作。

      有关此错误的更多信息可能在服务器错误日志中可用。

      html>
    • 我设法重定向,我必须在 Location 标头之后打印一个换行符以关闭 cgi 响应标头。现在我的问题是我丢失了重定向页面上帖子中的数据。
    • 不,不会的。
    【解决方案2】:

    1) shebang 在哪里? (#!/usr/bin/env python)

    2) 打印“错误” 是个问题。 cgi scripts stdout 是浏览器, “错误”这个词会有问题。

    3) chmod 755 脚本

    我抛出的重定向比网页多,这是我使用的功能。

    def togo(location):
        print "HTTP/1.1 302 Found"
        print "Location: ",location,"\r\n"
        print "Connection: close \r\n"
        print ""
    

    我不知道是否需要最后一个打印语句,并且 Connection close 标题对于大多数客户端来说似乎是可选的,但我将其保留是因为它应该在那里,我想。阅读 RFC 往往会让我入睡。

    当我第一次编写 cgi 脚本时,我不使用 cgi.FieldStorage,我对这些值进行了硬编码,以便可以在命令行上对其进行测试,在我开始工作后,我在浏览器中使用硬编码的值进行尝试,当它工作时,我添加了 cgi.FieldStorage。

    看看

    import cgitb
    cgitb.enable()
    

    我知道这可能会加重病情,我去过那里。 祝你好运。

    【讨论】:

    • 请澄清您的答案,目前尚不清楚您要做什么。
    猜你喜欢
    • 2018-02-08
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2014-02-22
    • 2011-04-08
    • 1970-01-01
    相关资源
    最近更新 更多