Python cgi.FieldStorage()总是返回一个空白表单,并且可以';无法从http POST读取参数

Python cgi.FieldStorage()总是返回一个空白表单,并且可以';无法从http POST读取参数,python,http,Python,Http,我试图获得一个基本的Python Web服务器示例,该示例从http POST接收数据。我目前正在使用Postman发送http POST,如下所示: 以下是我的Python脚本: # simple_web_server.py import SimpleHTTPServer import SocketServer import cgi ##############################################################################

我试图获得一个基本的Python Web服务器示例,该示例从http POST接收数据。我目前正在使用Postman发送http POST,如下所示:

以下是我的Python脚本:

# simple_web_server.py

import SimpleHTTPServer
import SocketServer
import cgi

###################################################################################################
def main():
    myRequestHandler = MyRequestHandler
    socketTCPServer = SocketServer.TCPServer(('0.0.0.0', 8080), myRequestHandler)
    print 'starting server, use <Ctrl-C> to stop'
    socketTCPServer.serve_forever()

###################################################################################################
class MyRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

    def do_GET(self):
        print "in do_GET(self)"
        return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

    def do_POST(self):
        print "in do_POST(self)"        # this shows successfully to the command line

        form = cgi.FieldStorage()

        print cgi.print_form(form)      # this always prints an empty form

        print str(form.getvalue('name'))        # this always prints an empty string

        if "name" not in form or "addr" not in form:        # the if never runs
            print "Please fill in the name and addr fields."
        else:
            print "<p>name:", form["name"].value
            print "<p>addr:", form["addr"].value

        self.wfile.write("test response 123 \n")    # this shows successfully in Postman
        self.send_response(200)                     # the 200 is successfully received by Postman

###################################################################################################
if __name__ == "__main__":
    main()
#simple_web_server.py
导入SimpleHTTPServer
导入SocketServer
导入cgi
###################################################################################################
def main():
myRequestHandler=myRequestHandler
sockettcserver=SocketServer.TCPServer(('0.0.0.0',8080),myRequestHandler)
打印“正在启动服务器,用于停止”
SockettcServer.serve_forever()
###################################################################################################
类MyRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_获得(自我):
打印“in do_GET(self)”
返回SimpleHTTPServer.SimpleHTTPRequestHandler.do\u GET(self)
def do_POST(自我):
打印“in do_POST(self)”;这将成功显示到命令行
form=cgi.FieldStorage()
打印cgi。打印表单(表单)#这总是打印空表单
print str(form.getvalue('name'))#这始终打印空字符串
如果“name”不在表单中或“addr”不在表单中:#if从不运行
打印“请填写姓名和地址字段。”
其他:
打印“name:”,表单[“name”]。值
打印“addr:”,表单[“addr”]。值
self.wfile.write(“test response 123\n”)#这在Postman中成功显示
self.send_response(200)#邮递员成功接收到200
###################################################################################################
如果名称=“\uuuuu main\uuuuuuuu”:
main()
以下是运行Python时的命令行结果:

starting server, use <Ctrl-C> to stop
127.0.0.1 - - [05/Jul/2016 20:02:33] "POST /?param1=value1&param2=value2 HTTP/1.1" 200 -
in do_POST(self)

<H3>Form Contents:</H3>
<P>No form fields.
<DL>
</DL>

None
None
Please fill in the name and addr fields.
启动服务器,用于停止
127.0.0.1---[2016年7月5日20:02:33]“POST/?param1=value1¶m2=value2 HTTP/1.1”200-
在do_POST(self)中
表格内容:

没有表单字段。 没有一个 没有一个 请填写姓名和地址字段。


从python.org文档和我能找到的每个示例中,我可以看出我做的一切都是正确的,但是表单数据总是空白的。最后,我需要下载一份邮件发送的文件,但我会推迟下载,直到我能阅读表格为止。还有其他人碰到过吗?非常感谢您的帮助。

我可能错了,但我认为如果您运行的是这样的基本服务器,那么您可以通过以下方式访问POST数据:

content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
post_data = self.rfile.read(content_length) # <--- Gets the data itself

您将在基本服务器类之外访问该。所以在main方法或类似的方法中。

我可能错了,但我认为如果您运行的是这样的基本服务器,那么您可以通过以下方式访问POST数据:

content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
post_data = self.rfile.read(content_length) # <--- Gets the data itself

您将在基本服务器类之外访问该。因此,在main方法或类似的方法中。

我应该提到,我只是尝试了BaseHTTPServer.BaseHTTPRequestHandler来代替上面的SimpleHTTPServer,没有什么区别。我还尝试了CGIHTTPServer.cgiHttpPrequestHandler,仍然是相同的结果我应该提到的是,我只是尝试了BaseHTTPServer.BaseHTTPRequestHandler来代替上面的SimpleHTTPServer,没有什么不同。我还尝试了CGIHTTPServer.CGIHTTPRequestHandler,仍然是相同的结果