Last active
June 23, 2018 14:04
-
-
Save bwangelme/09c5af16081015d10753da2978d8be67 to your computer and use it in GitHub Desktop.
Python Hello,World Web Server
This file contains hidden or 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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
from http.server import HTTPServer | |
from http.server import BaseHTTPRequestHandler | |
from http import HTTPStatus | |
class MyHandler(BaseHTTPRequestHandler): | |
def do_GET(self): | |
self.send_response(HTTPStatus.OK) | |
self.send_header('Content-type', 'text/html') | |
self.end_headers() | |
self.wfile.write(b'Hello, Python!') | |
return | |
def run(server_class=HTTPServer, handler_class=MyHandler): | |
server_address = ('localhost', 8000) | |
httpd = server_class(server_address, handler_class) | |
try: | |
print("Server works on http://localhost:8000") | |
httpd.serve_forever() | |
except KeyboardInterrupt: | |
print("Stop the server on http://localhost:8000") | |
httpd.socket.close() | |
if __name__ == '__main__': | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! that helped me understand localhost pretty well