Skip to content

Instantly share code, notes, and snippets.

@bradj
Last active Oct 25, 2018
Embed
What would you like to do?
Simple test stdlib webserver in py3.6
import time
from http.server import HTTPServer, BaseHTTPRequestHandler
HOST_NAME = ''
PORT_NUMBER = 8000
HTML_CONTENT = '''
<html><head><title>Title goes here.</title></head>
<body><p>This is a test.</p>
<p>You accessed path: %s</p>
</body></html>
'''
class MyHandler(BaseHTTPRequestHandler):
def do_HEAD(s):
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
def do_GET(s):
"""Respond to a GET request."""
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
s.wfile.write(bytes(HTML_CONTENT % s.path, 'utf-8'))
if __name__ == '__main__':
server_address = (HOST_NAME, PORT_NUMBER)
httpd = HTTPServer(server_address, MyHandler)
print(time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER))
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print(time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment