FastDeveloper
FastDeveloper

Reputation: 400

Implement both HTTP and HTTPS on my simple Python socket server

I want my visitors to be able to use both HTTP and HTTPS. I am using a simple Python webserver created with socket. I followed this guide: Python Simple SSL Socket Server, but it wasn't that helpful because the server would crash if the certificate cannot be trusted in one of the clients. Here is a few lines of code from my webserver that runs the server: def start(self): # create a socket object s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # bind the socket object to the address and port
    s.bind((self.host, self.port))
    # start listening for connections
    s.listen(100)

    print("Listening at", s.getsockname())
    while True:
        # accept any new connection
        conn, addr = s.accept()
        # read the data sent by the client (1024 bytes)
        data = conn.recv(1024).decode()
        pieces = data.split("\n")
        reqsplit = pieces[0].split(" ");
        # send back the data to client
        resp = self.handleRequests(pieces[0], pieces);
        conn.sendall(resp)

        # close the connection
        conn.close()

Upvotes: 1

Views: 698

Answers (1)

ibaguio
ibaguio

Reputation: 2368

Have another service (something like nginx) handle the https aspect, then configure the service to reverse proxy to your python server

Upvotes: 0

Related Questions