Skip to content

Instantly share code, notes, and snippets.

@RagingTiger
Forked from davidbgk/server.py
Last active August 2, 2022 11:36
Show Gist options
  • Save RagingTiger/f04d9b12792b6e6f1b25bf5ffb195790 to your computer and use it in GitHub Desktop.
Save RagingTiger/f04d9b12792b6e6f1b25bf5ffb195790 to your computer and use it in GitHub Desktop.
An attempt to create the simplest "sane" HTTP Hello world in Python3
import http.server
import socketserver
from http import HTTPStatus
# custom server
class SimpleServer(socketserver.TCPServer):
"""Run a simple server."""
def __init__(self, server_address, handler):
# notify server starting up
print('Server starting up ...')
# set reuse address
self.allow_reuse_address = True
# call super constructor of socketserver.TCPServer class
super().__init__(server_address, handler)
# notify started
print('Server now ready for requests!')
# create request handler
class RequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(HTTPStatus.OK)
self.end_headers()
self.wfile.write(b'Hello world')
# setup server vars
HOST, PORT = 'localhost', 9999
# Create the server, binding to localhost on port 9999
with SimpleServer((HOST, PORT), RequestHandler) as server:
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment