from __future__ import print_function from functools import partial from wsgiref.util import setup_testing_defaults from wsgiref.simple_server import make_server # A relatively simple WSGI application. def simple_app(environ, start_response): setup_testing_defaults(environ) status = '200 OK' log = partial(print, file=environ['wsgi.errors']) qs = environ['QUERY_STRING'] fw = environ['wsgi.file_wrapper'] if not qs: start_response('200 OK', [('Content-type', 'text/html; charset=utf-8')]) data = [] data.append('') data.append('
') for bttn in 'wsgi.file_wrapper', 'PEP 0333', 'slurp': input = '' % (bttn) data.append(input) data.append('
') return (item.encode() for item in data) start_response(status, [('Content-type', 'text/plain; charset=utf-8')]) with open(__file__) as myfile: log('QUERY_STRING =', qs) if 'slurp' in qs: return [ myfile.read().encode() ] elif fw and 'wsgi.file_wrapper' in qs: return fw(myfile) else: return iter(lambda: myfile.read(8192), '') httpd = make_server('', 8000, simple_app) print("Serving on port 8000...") httpd.serve_forever()