Skip to content

Instantly share code, notes, and snippets.

@schmir
Created September 7, 2010 21:22
Show Gist options
  • Save schmir/569134 to your computer and use it in GitHub Desktop.
Save schmir/569134 to your computer and use it in GitHub Desktop.
#!/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