Last active
October 25, 2018 18:59
-
-
Save bradj/5c09ef9d023c06a2810739ec651ae7d5 to your computer and use it in GitHub Desktop.
Simple test stdlib webserver in py3.6
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 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