Created
June 19, 2015 10:40
-
-
Save cdent/7126abd4a718a10e1d21 to your computer and use it in GitHub Desktop.
wsgi closing connections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Start with: | |
gunicorn app:application | |
or: | |
python app.py | |
Then: | |
curl -v -N http://127.0.0.1:8000/ | |
What you should see is a slow trickling of an upper case alphabet | |
and then a closed connection. | |
Interestingly gunicorn claims chunked Transfer-Encoding but wsgiref | |
does not. If you use telnet to make a raw GET / HTTP/1.1 request | |
you can see that gunicorn is using chunked encoding, wsgiref is not. | |
The behavior of the final output is not different. | |
""" | |
import sys | |
import time | |
from wsgiref.simple_server import make_server | |
def handler(environ, start_response): | |
start_response('200 OK', [('content-type', 'text/plain')]) | |
for i in 'abcdefhijklmnopqrstuvwxyz': | |
yield '%s\n' % i | |
time.sleep(0.5) | |
class UpperMiddleClass(object): | |
def __init__(self, application): | |
self.application = application | |
def __call__(self, environ, start_response): | |
return (output.upper() for output in | |
self.application(environ, start_response)) | |
application = UpperMiddleClass(handler) | |
if __name__ == '__main__': | |
httpd = make_server('', 8000, application) | |
try: | |
httpd.serve_forever() | |
except KeyboardInterrupt: | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment