-
-
Save schmir/569134 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# only works with patched gevent from http://github.com/schmir/gevent | |
from gevent import tlmonkey | |
tlmonkey.patch_all() | |
import gevent | |
from gevent import tpool | |
import sys | |
response = ["hello world\n"] | |
def application(environ, start_response): | |
start_response("200 OK", [("Content-Type", "text/html; charset=utf-8")]) | |
return response | |
def readline(): | |
while 1: | |
res = tpool.spawn(sys.stdin.readline) | |
line = res.get() | |
print "GOT LINE:", repr(line) | |
response.append(line) | |
if __name__ == "__main__": | |
from gevent.pywsgi import WSGIServer | |
address = "0.0.0.0", 8000 | |
server = WSGIServer(address, application) | |
gevent.spawn(readline) | |
try: | |
print "Server running on port %s:%d. Ctrl+C to quit." % address | |
server.serve_forever() | |
except KeyboardInterrupt: | |
server.stop() | |
print "Bye." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment