我似乎无法弄清楚如何使用WSGI访问POST数据。 我尝试了wsgi.org网站上的示例,但它不起作用。 我现在正在使用Python 3.0。 请不要推荐WSGI框架,因为这不是我想要的。
我想弄清楚如何将其转换为fieldstorage对象。
我似乎无法弄清楚如何使用WSGI访问POST数据。 我尝试了wsgi.org网站上的示例,但它不起作用。 我现在正在使用Python 3.0。 请不要推荐WSGI框架,因为这不是我想要的。
我想弄清楚如何将其转换为fieldstorage对象。
假设您正在尝试将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)
请注意,对于Python 3.0而言,WSGI尚未完全规范化,许多流行的WSGI基础设施尚未转换(或已经2to3d,但未经过适当的测试)。即使是wsgiref.simple_server也无法运行。如果今天要进行WSGI,则可能会遇到一些困难。
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()]
http://dev.pocoo.org/projects/werkzeug/browser/werkzeug/wrappers.py#L150
调用
http://dev.pocoo.org/projects/werkzeug/browser/werkzeug/utils.py#L1420
这个问题有点复杂,我就不在这里进行总结了。