Skip to content

Instantly share code, notes, and snippets.

@progrium
Created January 18, 2011 09:38
Show Gist options
  • Select an option

  • Save progrium/784205 to your computer and use it in GitHub Desktop.

Select an option

Save progrium/784205 to your computer and use it in GitHub Desktop.
import threading
from wsgiref.simple_server import make_server
def serve_once(handler, port=4442):
""" Single serving web server
Creates a web server in another thread that will output the result of
handler for a single request. Returns a URL to hit the web server.
"""
def app(e, start):
start("200 OK", [])
return [handler()]
httpd = make_server('', port, app)
threading.Thread(target=httpd.handle_request).start()
return "http://localhost:%s/" % port
if __name__ == '__main__':
import urllib
url = serve_once(lambda: 'foobar')
assert 'foobar' in urllib.urlopen(url).read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment