首页 > 解决方案 > 在我的简单 Python 套接字服务器上实现 HTTP 和 HTTPS

问题描述

我希望我的访问者能够同时使用 HTTP 和 HTTPS。我正在使用一个使用套接字创建的简单 Python 网络服务器。我遵循了本指南:Python Simple SSL Socket 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()

标签: pythonsockets

解决方案


让另一个服务(类似于 nginx)处理 https 方面,然后将服务配置为反向代理到您的 python 服务器


推荐阅读