-
-
Save RagingTiger/f04d9b12792b6e6f1b25bf5ffb195790 to your computer and use it in GitHub Desktop.
An attempt to create the simplest "sane" HTTP Hello world in Python3
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
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