import os
from http.server import BaseHTTPRequestHandler, HTTPServer


html = "<html><body>This is python http server</body></html>"

class ServerHandler(BaseHTTPRequestHandler):
    
    FILE_FW = 'fw.bin'

    def do_GET(self):
        print('Received GET request')
        if self.path == "/" + self.FILE_FW:
            with open(self.FILE_FW, 'rb') as file:
                file_stat = os.stat(self.FILE_FW)
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.send_header('Content-length', file_stat.st_size)
                self.end_headers()
                self.wfile.write(file.read())
        else:
            self.send_error(404, "Page Not Found {}".format(self.path))

def server_thread(port):
    server_address = ('localhost', port)
    httpd = HTTPServer(server_address, ServerHandler)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        httpd.server_close()
        print("Server stopped")

def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
    server_address = ('127.0.0.1', 9000)
    httpd = server_class(server_address, handler_class)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        httpd.server_name()


if __name__ == '__main__':
    run()
    # port = 8000
    # print("Starting server at port %d" % port)
    # server_thread(port)


# test()