Skip to content

Instantly share code, notes, and snippets.

@whatvn
Created April 17, 2017 10:22
Show Gist options
  • Save whatvn/2cc59935d525f2845292faadf3564c71 to your computer and use it in GitHub Desktop.
Save whatvn/2cc59935d525f2845292faadf3564c71 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
"""WSGI server example"""
from __future__ import print_function
from gevent.pywsgi import WSGIServer
import gevent
import sys
from time import *
from gevent.monkey import patch_all; patch_all()
def application(env, start_response):
if env['PATH_INFO'] == '/':
start_response('200 OK', [('Content-Type', 'text/html')])
# new thay gevent.sleep(5) bằng time.sleep(5) thì nó sẽ block, cho nên về nguyên tắc nếu muốn cái gì ko block thì phải wrap nó trong
# mấy cái wrapper của gevent
gevent.sleep(5)
return [b"<b>hello world</b>"]
else:
start_response('404 Not Found', [('Content-Type', 'text/html')])
return [b'<h1>Not Found</h1>']
base_env = {'GATEWAY_INTERFACE': 'CGI/1.1',
'SERVER_SOFTWARE': 'gevent/%d.%d Python/%d.%d' % (gevent.version_info[:2] + sys.version_info[:2]),
'SCRIPT_NAME': '',
'wsgi.version': (1, 0),
'wsgi.multithread': True, # XXX: Aren't we really, though?
'wsgi.multiprocess': False,
'wsgi.run_once': False}
if __name__ == '__main__':
print('Serving on 8088...')
WSGIServer(('', 8088), application, environ=base_env, backlog=1000, spawn=5).serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment