我似乎无法弄清楚如何使用 WSGI 访问 POST 数据。我在 wsgi.org 网站上尝试了该示例,但没有成功。我现在正在使用 Python 3.0。请不要推荐 WSGI 框架,因为那不是我想要的。
我想弄清楚如何将其放入字段存储对象中。
我似乎无法弄清楚如何使用 WSGI 访问 POST 数据。我在 wsgi.org 网站上尝试了该示例,但没有成功。我现在正在使用 Python 3.0。请不要推荐 WSGI 框架,因为那不是我想要的。
我想弄清楚如何将其放入字段存储对象中。
假设您正试图将 POST 数据放入 FieldStorage 对象:
# env is the environment handed to you by the WSGI server.
# I am removing the query string from the env before passing it to the
# FieldStorage so we only have POST data in there.
post_env = env.copy()
post_env['QUERY_STRING'] = ''
post = cgi.FieldStorage(
fp=env['wsgi.input'],
environ=post_env,
keep_blank_values=True
)
body= '' # b'' for consistency on Python 3.0
try:
length= int(environ.get('CONTENT_LENGTH', '0'))
except ValueError:
length= 0
if length!=0:
body= environ['wsgi.input'].read(length)
请注意,WSGI 尚未完全针对 Python 3.0 指定,并且许多流行的 WSGI 基础设施尚未转换(或已转换为 2to3d,但未经过适当测试)。(甚至 wsgiref.simple_server 也不会运行。)今天你在 3.0 上做 WSGI 很艰难。
这对我有用(在 Python 3.0 中):
import urllib.parse
post_input = urllib.parse.parse_qs(environ['wsgi.input'].readline().decode(),True)
我遇到了同样的问题,我花了一些时间研究解决方案。
带有详细信息和资源的完整答案(因为此处接受的答案在python3上对我不起作用,在 env 库等中需要纠正许多错误):
# the code below is taken from and explained officially here:
# https://wsgi.readthedocs.io/en/latest/specifications/handling_post_forms.html
import cgi
def is_post_request(environ):
if environ['REQUEST_METHOD'].upper() != 'POST':
return False
content_type = environ.get('CONTENT_TYPE', 'application/x-www-form-urlencoded')
return (content_type.startswith('application/x-www-form-urlencoded' or content_type.startswith('multipart/form-data')))
def get_post_form(environ):
assert is_post_request(environ)
input = environ['wsgi.input']
post_form = environ.get('wsgi.post_form')
if (post_form is not None
and post_form[0] is input):
return post_form[2]
# This must be done to avoid a bug in cgi.FieldStorage
environ.setdefault('QUERY_STRING', '')
fs = cgi.FieldStorage(fp=input,
environ=environ,
keep_blank_values=1)
new_input = InputProcessed()
post_form = (new_input, input, fs)
environ['wsgi.post_form'] = post_form
environ['wsgi.input'] = new_input
return fs
class InputProcessed(object):
def read(self, *args):
raise EOFError('The wsgi.input stream has already been consumed')
readline = readlines = __iter__ = read
# the basic and expected application function for wsgi
# get_post_form(environ) returns a FieldStorage object
# to access the values use the method .getvalue('the_key_name')
# this is explained officially here:
# https://docs.python.org/3/library/cgi.html
# if you don't know what are the keys, use .keys() method and loop through them
def application(environ, start_response):
start_response('200 OK', [('Content-type', 'text/plain')])
user = get_post_form(environ).getvalue('user')
password = get_post_form(environ).getvalue('password')
output = 'user is: '+user+' and password is: '+password
return [output.encode()]
我建议你看看一些框架是如何做的。(我不推荐任何一个,只是以它们为例。)
这是来自Werkzeug的代码:
http://dev.pocoo.org/projects/werkzeug/browser/werkzeug/wrappers.py#L150
调用
http://dev.pocoo.org/projects/werkzeug/browser/werkzeug/utils.py#L1420
在这里总结起来有点复杂,所以我不会。