如何解析“请求主体"?使用python CGI吗? [英] How to parse the "request body" using python CGI?
问题描述
我只需要编写一个简单的python CGI脚本来解析包含JSON的POST请求的内容.这只是测试代码,因此我可以测试客户端应用程序,直到实际服务器就绪(由其他人编写)为止.
I just need to write a simple python CGI script to parse the contents of a POST request containing JSON. This is only test code so that I can test a client application until the actual server is ready (written by someone else).
我可以读取cgi.FieldStorage()并转储keys(),但是找不到包含JSON的请求正文.
I can read the cgi.FieldStorage() and dump the keys() but the request body containing the JSON is nowhere to be found.
我也可以转储os.environ(),它提供了很多信息,但是我看不到包含请求正文的变量.
I can also dump the os.environ() which provides lots of info except that I do not see a variable containing the request body.
任何输入表示赞赏.
克里斯
推荐答案
如果您使用的是CGI,只需从stdin
中读取数据:
If you're using CGI, just read data from stdin
:
import sys
data = sys.stdin.read()
这篇关于如何解析“请求主体"?使用python CGI吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!