ssl Python中的TCP套接字在连续运行数小时后挂起

yqyhoc1h  于 2023-01-02  发布在  Python
关注(0)|答案(1)|浏览(109)

我有一个python脚本,它充当了一个服务器,但在某个地方挂起了。运行了大约24小时后,服务器似乎完全停止了侦听和响应。我不明白是什么原因导致了这个问题,因为它一直表现得非常正常,我一直试图写入日志,看看在它发生之前是否有任何异常发生。到目前为止,我还没有找到任何东西,我的理解是Python的TCP套接字会自动对客户端连接进行排队。

def ssl_server():
    #logger("MAIN", "ssl_server", "SERVER", "")
    #_logger_("MAIN", "ssl_server", "SERVER", "")
    
    global run

    run            = True
    non_ssl_thread = Thread(target = non_ssl_server, args = (server_config["enable-https"]["force-ssl"]["force"],server_config["enable-https"]["force-ssl"]["true-path"]))
    SERVER_HOST    = server_config["server-host"]
    tcp_socket     = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    
    try:
        if not(server_config["enable-https"]["enabled"]):
            raise Exception("HTTPS Disabled")
        
        SERVER_PORT = 443
        context     = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
        context.load_cert_chain(server_config["cert-location"])
        tcp_socket.bind((SERVER_HOST, SERVER_PORT))
        non_ssl_thread.daemon = True
        non_ssl_thread.start()
        tcp_socket.listen(server_config["queue-limit"])
        ssl_tcp_socket = context.wrap_socket(tcp_socket, server_side=True)
        listener(ssl_tcp_socket)
    except:
        #logger("MAIN", "ssl_server", "SERVER", "EXCEPTION: " + traceback.format_exc().replace('"', '&#34;').replace('<', "&#60;").replace('>', "&#62;").replace('\n', "<br>").replace(' ', "&#160;"))
        
        run = False
        tcp_socket.close()
        non_ssl_server()

    ssl_tcp_socket.close()
    
    try:
        tcp_socket.close()
    except:
        #logger("MAIN", "ssl_server", "SERVER", "EXCEPTION: " + traceback.format_exc().replace('"', '&#34;').replace('<', "&#60;").replace('>', "&#62;").replace('\n', "<br>").replace(' ', "&#160;"))
        pass
#END ssl_server()

def listener(_socket_, redirect_to_ssl = False, true_path = False):
    #logger("MAIN", "listener", "SERVER", "")
    #_logger_("MAIN", "listener", "SERVER", "")
    
    global run
    
    while run:
        try:
            (client_connection, client_address) = _socket_.accept()
            client_request                      = client_connection.recv(server_config["socket-buffer-size"]).decode()
            server_response                     = initialized_handler.respond_to_request(client_request, client_address, redirect_to_ssl, true_path)

            client_connection.sendall(server_response)
            client_connection.close()
        except:
            #logger("MAIN", "listener", "SERVER", "EXCEPTION: " + traceback.format_exc().replace('"', '&#34;').replace('<', "&#60;").replace('>', "&#62;").replace('\n', "<br>").replace(' ', "&#160;"))
            pass
#END listener()

我试过记录异常,记录客户端请求和服务器响应,据我所知,python脚本服务器似乎停止了侦听和响应,即使它仍然在“运行”。有趣的是,在单独线程中运行的非SSL TCP套接字从来没有挂起。而在主线程中运行的SSL TCP套接字挂起时,单独线程中的非SSL TCP套接字仍然接受客户端请求并响应它们,因为它们都使用相同的listener()函数,我只能假设它与Python SSL Package 器有关,但我不能确定。

6uxekuva

6uxekuva1#

听起来你所描述的行为可以用下面的代码来解释:run = Falsetcp_socket.close()non_ssl_server()您通过更改全局运行来终止线程进程,关闭套接字,然后启动另一个持续侦听的non_ssl_server。[DragonBobZ Dec 26,2022 at 22:28]

相关问题