Created
January 18, 2011 09:38
-
-
Save progrium/784205 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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