gistlib
main.pyfrom http.server import BaseHTTPRequestHandler, HTTPServer class MyHTTPHandler(BaseHTTPRequestHandler): def do_GET(self): if self.path == "/": self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() message = "Hello, world!" self.wfile.write(bytes(message, "utf8")) return def run(): port = 8000 server_address = ('', port) httpd = HTTPServer(server_address, MyHTTPHandler) print("Server running on port", port) httpd.serve_forever() if __name__ == '__main__': run() 614 chars22 lines
from http.server import BaseHTTPRequestHandler, HTTPServer class MyHTTPHandler(BaseHTTPRequestHandler): def do_GET(self): if self.path == "/": self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() message = "Hello, world!" self.wfile.write(bytes(message, "utf8")) return def run(): port = 8000 server_address = ('', port) httpd = HTTPServer(server_address, MyHTTPHandler) print("Server running on port", port) httpd.serve_forever() if __name__ == '__main__': run()
This code creates an HTTP server that responds to GET requests on the root path "/" with a "Hello, world!" message. The server listens on port 8000 by default, and can be started by running the script.
gistlibby LogSnag