Created
July 30, 2013 23:02
-
-
Save bdd/6117829 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 | |
import sys | |
import socket | |
import argparse | |
import BaseHTTPServer | |
from threading import Thread, Timer | |
class BasicServer(BaseHTTPServer.BaseHTTPRequestHandler): | |
def respond(self, code, body=""): | |
self.send_response(code) | |
self.send_header("Content-Type", "text/plain") | |
self.end_headers() | |
if len(body) > 0: | |
self.wfile.write(body) | |
def r200(self, body): | |
self.respond(200, body) | |
def r404(self, body="Not found."): | |
self.respond(404, body) | |
class AdminServer(BasicServer): | |
def do_GET(self): | |
if self.path == "/health": | |
self.r200("OK") | |
elif (self.path == "/quitquitquit" or | |
self.path == "/abortabortabort"): | |
self.r200("Shutting down...") | |
self.log_message("Got shutdown request from %s. Shutting down...", | |
self.client_address[0]) | |
Timer(0.1, self.server.shutdown).start() # I know, I know... | |
else: | |
self.r404() | |
class Server(BasicServer): | |
def do_GET(self): | |
self.r200("You reached host '%s', asked for path '%s'." % | |
(socket.gethostname(), self.path)) | |
def main(): | |
def serve(handler, port): | |
addr = ('', port) | |
BaseHTTPServer.HTTPServer(addr, handler).serve_forever() | |
argparser = argparse.ArgumentParser(description="example httpd") | |
argparser.add_argument('--http-port', dest='http_port', | |
action='store', type=int) | |
argparser.add_argument('--admin-port', dest='admin_port', | |
action='store', type=int) | |
args = argparser.parse_args() | |
admin_server = Thread(target=lambda: serve(AdminServer, args.admin_port)) | |
server = Thread(target=lambda: serve(Server, args.http_port)) | |
server.setDaemon(True) | |
server.start() | |
admin_server.start() | |
admin_server.join() | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment