Skip to content

Instantly share code, notes, and snippets.

@dtanham
Created September 26, 2014 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtanham/ac3e97e6109bef4379c3 to your computer and use it in GitHub Desktop.
Save dtanham/ac3e97e6109bef4379c3 to your computer and use it in GitHub Desktop.
200 OK Server
import BaseHTTPServer
import SocketServer
import socket
import threading
import functools
import contextlib
code, content = 200, "Ok"
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(code)
self.wfile.write("\n" + content )
@contextlib.contextmanager
def http_server(handler):
def url(port, path):
return 'http://%s:%s%s' % (socket.gethostname(), port, path)
httpd = SocketServer.TCPServer(("", 0), handler)
t = threading.Thread(target=httpd.serve_forever)
t.setDaemon(True)
t.start()
port = httpd.server_address[1]
yield functools.partial(url, port)
httpd.shutdown()
if __name__ == "__main__":
with http_server(Handler) as url:
print "Get me at: {}".format(url("/"))
while(1):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment