我可以从 python 中的环境变量中获取 POST 数据吗?

can I get the POST data from environment variables in python?

我试图理解 http 查询,并通过首先查看环境变量的键然后访问 'QUERY_STRING' 以获取实际数据,通过环境变量成功地从 GET 请求中获取数据。

像这样:

#!/usr/bin/python3
import sys
import cgi
import os

inputVars = cgi.FieldStorage()

f = open('test','w')
f.write(str(os.environ['QUERY_STRING])+"\n")
f.close()

有没有办法获取 POST 数据(相当于 POST 的 'QUERY_STRING' - 可以这么说)还是因为 POST 数据是用自己的包发送的?到目前为止,环境变量的键没有给我任何提示。

可能的重复项 link 解决了它,正如评论中指出的同义词和用户 Schien 在 linked 问题的答案之一中解释的那样:

原始http post数据(查询后的东西)可以通过stdin读取。 所以可以使用sys.stdin.read()方法。

我的代码现在看起来像这样:

#!/usr/bin/python3
import sys
import os

f = open('test','w')
f.write(str(sys.stdin.read()))
f.close()