无法使用cgi.FieldStorage获取POST值 [英] Cannot get POST values with cgi.FieldStorage

查看:1838
本文介绍了无法使用cgi.FieldStorage获取POST值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将带有表单数据的简单html页面发送给使用GET的用户,然后使用POST从表单接收变量。
HTML文件如下所示:

I`m trying to send simple html page with form data to user with GET, and then receive variables from form with POST. HTML file looks like:

<HTML>
<title> My Title</title>
<body>
<form  method="post" action="http.py">
<input name="Name" type="text"/>
<input name="Submit" type="submit" value="Submit" />
</form>
</body>
</HTML>

这是python脚本:

Here is python script:

import os
import cgi
import sys
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler



class customHTTPServer(BaseHTTPRequestHandler):
        def do_GET(self):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                fh=open('index.html','r')
                self.wfile.write(fh.read())
                return

        def do_POST(self):
            form = cgi.FieldStorage()
            self.send_response(200)
            self.end_headers()
            self.wfile.write(form['Name'].value)


def main():
       try:
            server = HTTPServer(('',9111),customHTTPServer)
            print 'server started at port 8080'
            server.serve_forever()
       except KeyboardInterrupt:
            server.socket.close()

if __name__=='__main__':
       sys.exit(main())

但FieldStorage一直保持空白。我已经尝试检查self.rfile中的内容,并发现如果我尝试执行self.rfile.readlines(),浏览器卡住并看起来像脚本正在等待数据流的结束。从哪里我应该使用POST提交的Name变量?

But FieldStorage remains empty all the time. I already tried to check what is in self.rfile, and found that if i try to do self.rfile.readlines() , browser stuck and looks like script is waiting for the end of data stream. From where i should take Name variable that i`m submitting with POST?

推荐答案

我想你可能会在这里混合一些概念。你既有服务器的想法,也有cgi脚本。无论你的POST动作是什么(http.py或其他),你的服务器都会接受请求。实际上没有CGI处理。因此,为了简单起见,您可以先调整html模板:

I think you might be mixing some concepts here. You have both the idea of a server and also a cgi script. No matter what your POST action is (http.py or whatever), your server is just going to take in a request. No CGI processing is actually happening. So you can first adjust your html template to this for simplicity:

<form  method="post" action="">

然后,你应该参考另一个关于如何从请求中读取,而不是尝试使用cgi fieldstorage:

Then, you should reference this other question about how to read from the request, as opposed to trying to use the cgi fieldstorage:

import urlparse

...

    def do_POST(self):
        length = int(self.headers.getheader('content-length'))
        postvars = urlparse.parse_qs(self.rfile.read(length), keep_blank_values=1)
        self.send_response(200)
        self.end_headers()
        self.wfile.write(postvars)

您遇到的问题,因为这是一个非常低级的方式创建一个Web服务器应用程序,如果您无限期地从输入流中读取它,它将继续运行并阻止。检查内容长度的标头,只读取那么多字节。您根本不需要cgi模块,因为这不是cgi脚本。

The issue you were having, and because this is a very low level way to create a web server app, if you read indefinitely from the input stream, it will keep going and block. You check the header for the content length and only read that many bytes. You don't have any need for the cgi module at all, because this is not a cgi script.

服务器的cgi脚本工作,看到请求是针对a匹配类型和位置的文件,并在像普通程序这样的子进程中执行。它为args提供进程,然后返回响应以返回给客户端。如果这是一个cgi脚本,服务器将在一个模块中,永远运行,而cgi代码将在另一个模块中使用更简单的代码来检查请求。

A cgi script works by the server seeing the request is for a file of a matching type and location, and executing it in a subprocess like a normal program. It feeds the process the args and then gets back a response to ship back to the client. If this were a cgi script, the server would be in one module, running forever, and the cgi code would be in another with much simpler code to check the request.

这篇关于无法使用cgi.FieldStorage获取POST值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆