Skip to content

Instantly share code, notes, and snippets.

@bwangelme
Last active June 23, 2018 14:04
Show Gist options
  • Save bwangelme/09c5af16081015d10753da2978d8be67 to your computer and use it in GitHub Desktop.
Save bwangelme/09c5af16081015d10753da2978d8be67 to your computer and use it in GitHub Desktop.
Python Hello,World Web Server
#!/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()
@yvcode
Copy link

yvcode commented May 30, 2018

Thanks! that helped me understand localhost pretty well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment