"""Simple WSGI applications
Adapted from ajp_test.py for WASD PyRTE
"""
def what(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['Applications are: app, test_app, \
hash_app, hash_app2, hash_app3, hash_app4\n']
def app(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['Hello World!\n']
def test_app(environ, start_response):
import cgi
start_response('200 OK', [('Content-Type', 'text/html')])
yield '
Hello World!\n' \
'\n' \
'Hello World!
\n' \
''
names = environ.keys()
names.sort()
for name in names:
yield '%s | %s |
\n' % (
name, cgi.escape(`environ[name]`))
form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ,
keep_blank_values=1)
if form.list:
yield 'Form data |
'
for field in form.list:
yield '%s | %s |
\n' % (
field.name, field.value)
yield '
\n' \
'\n'
# For testing the InputStream implementation (use curl --data-binary)
def hash_app(environ, start_response):
import sha
h = sha.sha()
h.update(environ['wsgi.input'].read())
start_response('200 OK', [('Content-Type', 'text/plain')])
return [h.hexdigest() + '\n']
def hash_app2(environ, start_response):
import sha
h = sha.sha()
inf = environ['wsgi.input']
s = inf.read(4096)
while s:
h.update(s)
s = inf.read(4096)
start_response('200 OK', [('Content-Type', 'text/plain')])
return [h.hexdigest() + '\n']
def hash_app3(environ, start_response):
import sha
h = sha.sha()
lines = 0
for line in environ['wsgi.input']:
h.update(line)
lines += 1
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['%s\n%s\n' % (h.hexdigest(), lines)]
def hash_app4(environ, start_response):
import sha
h = sha.sha()
lines = 0
inf = environ['wsgi.input']
line = inf.readline(100)
while line:
h.update(line)
lines += 1
line = inf.readline(100)
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['%s\n%s\n' % (h.hexdigest(), lines)]
def wrap_app(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
f = open('wsgi.c')
if 'wsgi.file_wrapper' in environ:
return environ['wsgi.file_wrapper'](f)
else:
return iter(f.read(4096), '')
# Wrap a file-like object that doesn't support fileno()
def wrap_app2(environ, start_response):
import StringIO
start_response('200 OK', [('Content-Type', 'text/plain')])
f = open('wsgi.c')
s = f.read()
f.close()
f = StringIO.StringIO()
f.write(s)
f.seek(0, 0)
if 'wsgi.file_wrapper' in environ:
return environ['wsgi.file_wrapper'](f)
else:
return iter(f.read(4096), '')
import wasd
from wsgiref.handlers import CGIHandler
# not elegant (but hey)
qap = wasd.getvar('query_string')
if qap == 'app':
CGIHandler().run(app)
elif qap == 'test_app':
CGIHandler().run(test_app)
elif qap == 'hash_app':
CGIHandler().run(hash_app)
elif qap == 'hash_app2':
CGIHandler().run(hash_app2)
elif qap == 'hash_app3':
CGIHandler().run(hash_app3)
elif qap == 'hash_app4':
CGIHandler().run(hash_app4)
else:
CGIHandler().run(what)