[英]Python cgi.FieldStorage() always returns a blank form, and can't read params from http POST
I'm attempting to get a basic Python webserver example going that receives data from a http POST. 我正在尝试获取一个基本的Python网络服务器示例,该示例从http POST接收数据。 I'm currently using Postman to send the http POST as follows:
我目前正在使用Postman发送HTTP POST,如下所示:
Here is my Python script: 这是我的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()
Here are the command line results from Python when ran: 这是运行时Python的命令行结果:
starting server, use <Ctrl-C> to stop
127.0.0.1 - - [05/Jul/2016 20:02:33] "POST /?param1=value1¶m2=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.
As far as I can tell from the python.org docs and every example I could find I'm doing everything correctly but the form data is always blank. 据我从python.org文档和每个示例可以看出,我发现我做的一切正确,但是表单数据始终为空。 Eventually I need to download a file sent by the POST but I'm going to hold off on that until I can read the form at all.
最终,我需要下载由POST发送的文件,但我将坚持下去,直到我完全可以阅读该表格为止。 Has anybody else ran into this?
有人遇到过这个吗? Any assistance would be greatly appreciated.
任何帮助将不胜感激。
I could be wrong but I think if you're running a base server like this, then you access the POST data via: 我可能是错的,但我认为如果您正在运行这样的基本服务器,则可以通过以下方式访问POST数据:
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
post_data = self.rfile.read(content_length) # <--- Gets the data itself
If you wanted to access the POST data using 如果您想使用以下方式访问POST数据
form = cgi.FieldStorage()
you would access that outside the base server class. 您可以在基本服务器类之外访问该类。 So in the main method or something like that.
所以在主要方法或类似的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.