BaseHTTPRequestHandle sometimes not respond

I am not good at networking.
creating a simple python public server for my personal use, only need to use do_GET.

I have few devices to access it.
it works at the beginning, but after a while, or after requested by different devices.
it will not respond sometimes, it seems blocking by one of my devices or something else.
but nothing useful show in the console.

what am I doing wrong?

from http.server import BaseHTTPRequestHandler, HTTPServer
import ssl
import custom_website_manager

class MyServer(BaseHTTPRequestHandler):
    def do_GET(self):
        try:
            respond_code, resopond_type, resopond_content = custom_website_manager.do_GET(self.path, self.client_address)
            if respond_code:
                self.send_response(respond_code)
                self.end_headers()
                self.wfile.write(resopond_content)
            else:
                self.send_response(404)
                self.end_headers()
        except Exception as e:
            pass
        
if __name__ == "__main__":        
    webServer = HTTPServer(('0.0.0.0', 443), MyServer)
    key_pem = r'ssl\v3\cert-key.pem'
    cert_pem = r'ssl\v3\cert.pem'
    webServer.socket = ssl.wrap_socket(webServer.socket, keyfile = key_pem, certfile= cert_pem, server_side= True)

    try:
        webServer.serve_forever()
    except KeyboardInterrupt:
        webServer.server_close()

What you’re using is a VERY basic server. It doesn’t handle concurrent clients well. I would recommend trying ThreadingHTTPServer instead of HTTPServer, but even that isn’t really production-optimized, and definitely isn’t hardened for use as a public server. Instead, look into using a dedicated HTTP server like Apache or nginx, and using a proper web app library like Flask, Django, Bottle, etc. It’ll be a bit more effort to learn how to set that up, but it’s a very saleable skill, and you’ll have something extensible and much much more reliable.

(Why do I say this? Because, years and years ago, I set a thing up basically like you’re doing here, and it needed more and more extensions until it was an utter pain to manage. That was only used by trusted users on a LAN, so the security issues weren’t a problem, but it did make me regret not starting with Flask.)

1 Like

thank you for your help.

I though keeping things simple will be easier.
but you are right, I will check on ngnix and Flask.

It definitely is! That’s why simple options like http.server exist. But they’re not designed to be able to do everything, so when - like this - you run into problems, it’s usually better to jump to a proper server and application stack than to try to patch in better support within a non-hardened app.

I ended up using fastAPI and was very satisfied with its performance.

Thanks for your help, it’s much appreciated!

1 Like

Awesome, good to hear! All the best.