使用 mod_wsgi 的 Python POST 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/394465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python POST data using mod_wsgi
提问by Silver Dragon
This must be a very simple question, but I don't seem to be able to figure out.
这一定是一个非常简单的问题,但我似乎无法弄清楚。
I'm using apache + mod_wsgi to host my python application, and I'd like to get the post content submitted in one of the forms -however, neither the environment values, nor sys.stdin contains any of this data. Mind giving me a quick hand?
我正在使用 apache + mod_wsgi 来托管我的 python 应用程序,并且我希望以其中一种形式提交帖子内容 - 但是,环境值和 sys.stdin 都不包含任何这些数据。介意帮我一把吗?
Edit: Tried already:
编辑:已经尝试过:
- environ["CONTENT_TYPE"] = 'application/x-www-form-urlencoded' (no data)
- environ["wsgi.input"] seems a plausible way, however, both environ["wsgi.input"].read(), and environ["wsgi.input"].read(-1) returns an empty string (yes, content has been posted, and environ["request_method"] = "post"
- 环境 ["CONTENT_TYPE"] = 'application/x-www-form-urlencoded'(无数据)
- 环境 ["wsgi.input"] 似乎是一种合理的方式,但是,环境 ["wsgi.input"].read() 和环境 ["wsgi.input"].read(-1) 都返回一个空字符串(是的, 内容已发布,且environ["request_method"] = "post"
回答by nosklo
PEP 333says you must read environ['wsgi.input'].
PEP 333说你必须阅读 environment['wsgi.input']。
I just saved the following code and made apache's mod_wsgi run it. It works.
我刚刚保存了以下代码并让 apache 的 mod_wsgi 运行它。有用。
You must be doing something wrong.
你一定是做错了什么。
from pprint import pformat
def application(environ, start_response):
# show the environment:
output = ['<pre>']
output.append(pformat(environ))
output.append('</pre>')
#create a simple form:
output.append('<form method="post">')
output.append('<input type="text" name="test">')
output.append('<input type="submit">')
output.append('</form>')
if environ['REQUEST_METHOD'] == 'POST':
# show form data as received by POST:
output.append('<h1>FORM DATA</h1>')
output.append(pformat(environ['wsgi.input'].read()))
# send results
output_len = sum(len(line) for line in output)
start_response('200 OK', [('Content-type', 'text/html'),
('Content-Length', str(output_len))])
return output
回答by Graham Dumpleton
Be aware that technically speaking calling read() or read(-1) on wsgi.input is a violation of the WSGI specification even though Apache/mod_wsgi allows it. This is because the WSGI specification requires that a valid length argument be supplied. The WSGI specification also says you shouldn't read more data than is specified by the CONTENT_LENGTH.
请注意,从技术上讲,在 wsgi.input 上调用 read() 或 read(-1) 违反了 WSGI 规范,即使 Apache/mod_wsgi 允许这样做。这是因为 WSGI 规范要求提供有效的长度参数。WSGI 规范还说你不应该读取比 CONTENT_LENGTH 指定的更多的数据。
So, the code above may work in Apache/mod_wsgi but it isn't portable WSGI code and will fail on some other WSGI implementations. To be correct, determine request content length and supply that value to read().
因此,上面的代码可以在 Apache/mod_wsgi 中工作,但它不是可移植的 WSGI 代码,并且会在其他一些 WSGI 实现上失败。要正确,请确定请求内容长度并将该值提供给 read()。