Skip to content

Instantly share code, notes, and snippets.

@ilatypov
Last active February 13, 2017 00:13
Show Gist options
  • Save ilatypov/2de85ab4dcaf186ee7e2eabe261c1e8d to your computer and use it in GitHub Desktop.
Save ilatypov/2de85ab4dcaf186ee7e2eabe261c1e8d to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# vim: et:ts=4:sts=4:sw=4:fileencoding=utf-8
ur"""
A sample WSGI application.
http://lucumr.pocoo.org/2007/5/21/getting-started-with-wsgi/
"""
import urlparse
html_escape_table = {
"&": "&",
'"': """,
"'": "'",
">": ">",
"<": "&lt;",
}
def html_escape(text):
"""Produce entities within text."""
return "".join(html_escape_table.get(c,c) for c in text)
def mod_wsgi_187(environ, start_response):
# for (k, v) in sorted(environ.items()):
# print "\"%s\"=\"%s\"" % (k, v, )
# In the standalone mode SCRIPT_NAME shows empty, PATH_INFO receives "/mod_wsgi_187".
# When integrated with Apache, SCRIPT_NAME receives "/mod_wsgi_187", PATH_INFO shows empty.
path = environ.get('SCRIPT_NAME', '') + environ.get('PATH_INFO', '')
if path != '/mod_wsgi_187':
start_response('404 Not Found', [])
return ''
start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])
parameters = urlparse.parse_qs(environ.get('QUERY_STRING', ''))
if 'subject' in parameters:
subject = parameters['subject'][0]
else:
subject = 'World'
try:
request_body_size = int(environ.get('CONTENT_LENGTH', 0))
except ValueError:
request_body_size = 0
stream = environ.get('wsgi.input')
print dir(stream)
to_read = request_body_size
limit = 10240
buf = []
req_info = []
while to_read > 0:
chunk_size = to_read
if chunk_size > limit:
chunk_size = limit
received = stream.read(chunk_size)
len_received = len(received)
if len_received == 0:
raise Exception('EOF before reading %d more bytes' % (to_read,))
buf.append(received)
to_read -= len_received
req_info.append((chunk_size, len_received))
wsgi_input = []
query = ''.join(buf)
if len(query) > 0:
d = urlparse.parse_qsl(query, keep_blank_values=True, strict_parsing=True)
else:
d = ()
sorted_input = sorted(d, key=lambda x: (repr(x[0]).lower(), repr(x[1]).lower()))
test_value = 'x' * (10241 - len('username=&submit=Submit+Query')); # 10212
for key, value in sorted_input:
if key == "username":
test_value = value
wsgi_input.append('<tr><td>%s</td><td><code>%s</code></td></tr>' % (
html_escape(key), html_escape(value)))
return ['''<html>
<head>
<title>Hello %(subject)s</title>
<style>
table {
border: 1px solid grey;
border-collapse: collapse;
width: 100%%;
table-layout: fixed;
}
th, td {
border: 1px solid grey;
word-wrap: break-word;
}
th.name {
width: 100px;
}
</style>
</head>
<body>
Hello %(subject)s!
<p>
%(req_info)s
<p>
<table>
<thead>
<tr><th class="name">Name</th><th class="value">Value</th></tr>
</thead>
<tbody>
%(wsgi_input)s
</tbody>
</table>
<p>
<form method="POST">
<input type="text" value="%(test_value)s" name="username"/>
Length: %(test_value_len)s
<input type="submit" name="submit"/>
</form>
</body>
</html>
''' % {
'subject': html_escape(subject),
'req_info': '<br>'.join(html_escape('next chunk: %d, received: %d' % (chunk_size, len_received,))
for (chunk_size, len_received) in req_info),
'wsgi_input': '\n'.join(wsgi_input),
'test_value': html_escape(test_value),
'test_value_len': len(test_value),
}]
application = mod_wsgi_187
if __name__ == '__main__':
from wsgiref.simple_server import make_server
srv = make_server('localhost', 18080, mod_wsgi_187)
srv.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment