< Applied Programming < Web Design < Python3

main.py

"""
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.html

<!DOCTYPE html>
<html lang="en">
<body>
<h1>Test</h1>
</body>
</html>
This article is issued from Wikiversity. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.