python - 超时 SSL 握手

标签 python ssl twisted connection-timeout

我正在尝试将 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/

相关文章:

python - 如何使用 Twisted Words Python 库创建一个简单的 Google Talk 客户端?

Scrapy `ReactorNotRestartable` : one class to run two (or more) spiders

python - 导入错误 : dynamic module does not define init function (initfizzbuzz)

python - 在 Z3py 中使用多字节字符作为函数和排序名称

python - 奇怪的python比较行为

SSL 网站在非 SSL (HTTP) 上显示 404 错误

c# - 通过非安全通道加载内容

python - 修改 `objects` 以*始终*返回对象的子集?

ssl - 无需 SSL 的 IIS 多对一证书

python - 序列化twisted.protocols.amp.AmpList以进行测试