1
# Taken from <http://www.python.org/dev/peps/pep-0333/>
2
# which was placed in the public domain.
7
__all__ = ['WSGIServer']
10
class WSGIServer(object):
12
def __init__(self, application):
13
self.application = application
17
environ = dict(os.environ.items())
18
environ['wsgi.input'] = sys.stdin
19
environ['wsgi.errors'] = sys.stderr
20
environ['wsgi.version'] = (1,0)
21
environ['wsgi.multithread'] = False
22
environ['wsgi.multiprocess'] = True
23
environ['wsgi.run_once'] = True
25
if environ.get('HTTPS','off') in ('on','1'):
26
environ['wsgi.url_scheme'] = 'https'
28
environ['wsgi.url_scheme'] = 'http'
35
raise AssertionError("write() before start_response()")
37
elif not headers_sent:
38
# Before the first output, send the stored headers
39
status, response_headers = headers_sent[:] = headers_set
40
sys.stdout.write('Status: %s\r\n' % status)
41
for header in response_headers:
42
sys.stdout.write('%s: %s\r\n' % header)
43
sys.stdout.write('\r\n')
45
sys.stdout.write(data)
48
def start_response(status,response_headers,exc_info=None):
52
# Re-raise original exception if headers sent
53
raise exc_info[0], exc_info[1], exc_info[2]
55
exc_info = None # avoid dangling circular ref
57
raise AssertionError("Headers already set!")
59
headers_set[:] = [status,response_headers]
62
result = self.application(environ, start_response)
65
if data: # don't send headers until body appears
68
write('') # send headers now if body was empty
70
if hasattr(result,'close'):