我正在尝试将 TimeoutMixin
合并到基于 SSL 的协议(protocol)中。但是,当超时发生并且它调用 transport.loseConnection()
时,什么也没有发生。我认为这与 this code in TLSMemoryBIOProtocol 有关:
def _shutdownTLS(self):
"""
Initiate, or reply to, the shutdown handshake of the TLS layer.
"""
try:
shutdownSuccess = self._tlsConnection.shutdown()
except Error:
# Mid-handshake, a call to shutdown() can result in a
# WantWantReadError, or rather an SSL_ERR_WANT_READ; but pyOpenSSL
# doesn't allow us to get at the error. See:
# https://github.com/pyca/pyopenssl/issues/91
shutdownSuccess = False
self._flushSendBIO()
if shutdownSuccess:
# Both sides have shutdown, so we can start closing lower-level
# transport. This will also happen if we haven't started
# negotiation at all yet, in which case shutdown succeeds
# immediately.
self.transport.loseConnection()
问题是在握手发生之前发生了超时。在服务器端,它有一个打开的端口用于监听连接,但服务器被卡住,无法进行正确的握手。该代码片段看起来无法执行 TLS 关闭,然后什么也不做。
我的问题是:
如何设置 SSL 握手超时?如果握手没有在合理的时间内发生,如何正确断开连接?此外,无论 TLS 连接是否断开,将上述代码段更改为删除底层较低级别的连接是否有任何问题? (只是什么都不做,无限期地挂起似乎不是正确的方法)
编辑:
调用loseConnection
的失败似乎发生在之前发送过任何数据或者没有发送任何数据的情况下似乎工作正常。
最佳答案
loseConnection
是用于有序关闭连接的 API。如果您想突然终止连接,abortConnection
是适合您的 API。
关于python - 超时 SSL 握手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27583753/