首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扭曲:将ReconnectingClientFactory与SSL write()混合使用会频繁引发ssl错误。

扭曲:将ReconnectingClientFactory与SSL write()混合使用会频繁引发ssl错误。
EN

Stack Overflow用户
提问于 2017-02-15 19:41:34
回答 1查看 125关注 0票数 0

我正在继承ReconnectingClientFactory并使用SSL连接。我们已经将其全部运行,但几分钟后我收到以下错误。一旦发生此错误,连接就会丢失

代码语言:javascript
复制
Unhandled Error
Traceback (most recent call last):
  File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/python/log.py", line 103, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/python/log.py", line 86, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
--- <exception caught here> ---
  File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
    why = selectable.doRead()
  File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/internet/tcp.py", line 208, in doRead
    return self._dataReceived(data)
  File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/internet/tcp.py", line 214, in _dataReceived
    rval = self.protocol.dataReceived(data)
  File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/protocols/tls.py", line 430, in dataReceived
    self._flushReceiveBIO()
  File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/protocols/tls.py", line 400, in _flushReceiveBIO
    self._flushSendBIO()
  File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/protocols/tls.py", line 352, in _flushSendBIO
    bytes = self._tlsConnection.bio_read(2 ** 15)
  File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/OpenSSL/SSL.py", line 1384, in bio_read
    self._handle_bio_errors(self._from_ssl, result)
  File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/OpenSSL/SSL.py", line 1365, in _handle_bio_errors
    _raise_current_error()
  File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/OpenSSL/_util.py", line 48, in exception_from_error_queue
    raise exception_type(errors)
OpenSSL.SSL.Error: []

我的客户端代码如下:

代码语言:javascript
复制
reactor.connectSSL(opscenter_addr, int(opscenter_port), NoahAgentFactory(), ssl.ClientContextFactory())

NoahAgentFactory类如下所示:

代码语言:javascript
复制
import logging
import traceback

from OpenSSL import SSL
from twisted.internet import reactor,ssl
from twisted.internet import task
from twisted.internet.protocol import Protocol
from twisted.internet.protocol import ReconnectingClientFactory
import DataPackageCodecFactory as Codec
from Header import MessageHandlerMap
from Handler import HBReq, DefaultRspHandler
from Util import Connection

logger = logging.getLogger('opsagent')


class NoahAgentProtocol(Protocol):
    t = None
    def connectionMade(self):
        '''
            客户端连接成功之后会自动调用该方法
            :return:
        '''
        self.transport.setTcpKeepAlive(True) # maintain the TCP connection
        self.transport.setTcpNoDelay(True) # allow Nagle algorithm

        # 连接成功后保存连接信息
        Connection.AgentTcpConnection = self
        global t
        logger.info('is already connect to the server')
        self.recv_data_buffer = ''
        # 创建定时的心跳任务,每隔30秒执行一次
        t = task.LoopingCall(HBReq.execute, *[self])
        t.start(30)

    def dataReceived(self, data):
        logger.debug("Received Message: %s", repr(data))
        ###code handler packages##########

    def connectionLost(self, reason):
        '''
        当客户端连接断开的时候,会自动调用该方法
        :param reason:
        :return:
        '''
        try:
            t.stop()
            # 清空全局链接信息
            Connection.AgentTcpConnection = None
        except:
            logger.error(traceback.format_exc())
        logger.info('Connection is lost and Task stopped,Resaon =>%s', reason)


class NoahAgentFactory(ReconnectingClientFactory):
    def startedConnecting(self, connector):
        logger.info('Started to connect.')

    def buildProtocol(self, addr):
        logger.info('Connected.')
        logger.info('Resetting reconnection delay')
        self.resetDelay()
        return NoahAgentProtocol()

    def clientConnectionLost(self, connector, reason):
        logger.info('Lost connection.  Reason:%s', reason)
        self.resetDelay()
        self.retry(connector)
        # ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

    def clientConnectionFailed(self, connector, reason):
        logger.info('Connection failed. Reason:%s', reason)
        self.resetDelay()
        self.retry(connector)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-20 16:12:31

我认为这不是twisted的TLS中的错误,而是一个错误,我发送的消息不在主反应器线程中,因为我们知道,扭曲的是单线程模式,所以没有必要考虑线程安全我使用reactor.callInThread来处理长时间开销的业务并直接发送消息,而issues.But ()不是线程安全的,所以调用那个错误,一旦我在发送消息上使用了reactor.callFromThread,它就可以正常运行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42248394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档