Skip to content

Instantly share code, notes, and snippets.

@jorisdevrede
Created April 29, 2018 12:45
Show Gist options
  • Save jorisdevrede/f94273c747f487a461a72c8ea8bcb0dc to your computer and use it in GitHub Desktop.
Save jorisdevrede/f94273c747f487a461a72c8ea8bcb0dc to your computer and use it in GitHub Desktop.
HTTP Server
from argparse import ArgumentParser
from configparser import ConfigParser
from http.server import BaseHTTPRequestHandler
from socketserver import TCPServer
import time
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.server.output['time'] = time.asctime()
output = ""
for key in self.server.output:
output = output + key + ": " + self.server.output[key] + "\n"
self.wfile.write(output.encode())
def start(port):
httpd = TCPServer(("", port), MyHandler)
httpd.output = {}
httpd.output['name'] = 'My HTTP Server'
httpd.serve_forever()
if __name__ == "__main__":
parser = ArgumentParser(description='Runs a custom HTTP Server')
parser.add_argument('--config', dest='config', type=str, help='ini configuration file')
args = parser.parse_args()
config = ConfigParser()
config.read(args.config)
port = int(config['default']['port'])
start(port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment