Applied Programming/Web Design/Python3/http server

main.pyEdit

"""
references:
* https://www.youtube.com/watch?v=hFNZ6kdBgO0
* https://docs.python.org/3/library/http.server.html
* https://github.com/howCodeORG/Simple-Python-Web-Server
"""
from http.server import HTTPServer, BaseHTTPRequestHandler


class Serve(BaseHTTPRequestHandler):

    def do_GET(self):
        if self.path == '/':
            self.path = '/index.html'
        try:
            file_to_open = open(self.path[1:]).read()
            self.send_response(200)
        except:
            file_to_open = "File not found"
            self.send_response(404)
        self.end_headers()
        self.wfile.write(bytes(file_to_open, 'utf-8'))


httpd = HTTPServer(('localhost', 5000), Serve)
httpd.serve_forever()


index.htmlEdit

<!DOCTYPE html>
<html lang="en">
<body>
<h1>Test</h1>
</body>
</html>