gpt4 book ai didi

python - 连接失败后扭曲延迟不可调用

转载 作者:行者123 更新时间:2023-11-28 17:52:23 25 4
gpt4 key购买 nike

我的客户工厂在这里:

import logging, traceback
from twisted.internet.protocol import ClientFactory
from twisted.internet import defer, reactor, ssl
from twisted.application import service
from protocols.smpp.protocol import SMPPClientProtocol

class SMPPClientFactory(ClientFactory):

protocol = SMPPClientProtocol

def __init__(self, config):
self.config = config

def getConfig(self):
return self.config

def clientConnectionFailed(self, connector, reason):
print "clientConnectionFailed"

self.connectDeferred.errback(reason)

def clientConnectionLost(self, connector, reason):
print "clientConnectionLost"

def connect(self):
self.connectDeferred = defer.Deferred()
factory = SMPPClientFactory(self.config, self.msgHandler)

self.log.warning('Establishing TCP connection to %s:%d' % (self.config.host, self.config.port))
reactor.connectTCP(self.config.host, self.config.port, factory)

return self.connectDeferred

它在这里启动代码:

import logging, traceback
from twisted.internet import reactor, defer
from protocols.smpp.configs import SMPPClientConfig
from protocols.smpp.smpp_operations import SMPPOperationFactory
from testbed.client import SMPPClientFactory

class SMPP(object):

def __init__(self, config=None):
if config is None:
config = SMPPClientConfig()

self.config = config
self.opFactory = SMPPOperationFactory(config)

def run(self):
try:
#Bind
SMPPClientFactory(self.config, self.handleMsg).connect().addErrback(self.connectFailed)
except Exception, e:
print "ERROR: %s" % str(e)

def connectFailed(self, reason):
print "Connection failed %s" % str(reason)

def handleMsg(self, smpp, pdu):
pass

if __name__ == '__main__':
config = SMPPClientConfig(host='127.0.0.1', port=2775, username='smppclient1', password='password',
log_level=logging.DEBUG)

logging.basicConfig(level=config.log_level, filename=config.log_file, format=config.log_format,datefmt=config.log_dateformat)
SMPP(config).run()
reactor.run()

当连接失败(远程服务器关闭)时,我得到工厂的 clientConnectionFailed 调用,但奇怪的是收到“exceptions.AttributeError:SMPPClientFactory 实例没有属性‘connectDeferred’”。

我需要在连接失败时调用 errback,在处理 deferreds 时似乎缺少了一些东西..

最佳答案

在您的启动代码中,您实例化了一个 SMPPClientFactory 并在其上调用了 connect()。这个特定实例将具有 connectDeferred 属性。但是,connect 还实例化了另一个 SMPPClientFactory:factory = SMPPClientFactory(self.config, self.msgHandler) 这是您用来创建实际连接的实例。这没有 connectDeferred 属性,因为此实例从未调用过连接。

关于python - 连接失败后扭曲延迟不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7368891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com