wsgiref — WSGI Utilities and Reference Implementation nginx


from wsgiref.util import setup_testing_defaults, request_uri
from wsgiref.simple_server import make_server


# A relatively simple WSGI application. It's going to print out the
# environment dictionary after being updated by setup_testing_defaults
def simple_app(environ, start_response):
setup_testing_defaults(environ)

status = '200 OK'
headers = [('Content-type', 'text/plain; charset=utf-8')]

start_response(status, headers)
r_uri = request_uri(environ, include_query=True)
if 'test_dump' in r_uri:
return [("%s: %s\n" % (key, value)).encode('utf-8') for key, value in environ.items()]
elif 'uid' in r_uri:

try:
uid = int(r_uri.split('/')[-1])
if uid > 0:
r_l = gen_r(uid)
return [("%s: %s\n" % (i, r_l[i])).encode('utf-8') for i in range(len(r_l))]
else:
return [('%s%s' % ('BAD-REQUEST ', r_uri)).encode('utf-8')]
except Exception as e:
return [('%s%s%s' % (e, ' retry...,please.... ', r_uri)).encode('utf-8')]
else:
return [('%s%s' % ('dev-ING- ', r_uri)).encode('utf-8')]


with make_server('', 8000, simple_app) as httpd:
print("Serving on port 8000...")
httpd.serve_forever()


posted @ 2017-12-15 11:20  papering  阅读(253)  评论(0编辑  收藏  举报