diff --git a/src/python/scripts/wsgiref_test3.py b/src/python/scripts/wsgiref_test3.py new file mode 100755 index 0000000000000000000000000000000000000000..1a665681e650d459a34769dca9528c29e2a4af52_c3JjL3B5dGhvbi9zY3JpcHRzL3dzZ2lyZWZfdGVzdDMucHk= --- /dev/null +++ b/src/python/scripts/wsgiref_test3.py @@ -0,0 +1,125 @@ +"""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 '<html><head><title>Hello World!</title></head>\n' \ + '<body>\n' \ + '<p>Hello World!</p>\n' \ + '<table border="1">' + names = environ.keys() + names.sort() + for name in names: + yield '<tr><td>%s</td><td>%s</td></tr>\n' % ( + name, cgi.escape(`environ[name]`)) + + form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ, + keep_blank_values=1) + if form.list: + yield '<tr><th colspan="2">Form data</th></tr>' + + for field in form.list: + yield '<tr><td>%s</td><td>%s</td></tr>\n' % ( + field.name, field.value) + + yield '</table>\n' \ + '</body></html>\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) +