Guest User

Untitled

a guest
Aug 25th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. Twisted deferred not callable after connection failure
  2. import logging, traceback
  3. from twisted.internet.protocol import ClientFactory
  4. from twisted.internet import defer, reactor, ssl
  5. from twisted.application import service
  6. from protocols.smpp.protocol import SMPPClientProtocol
  7.  
  8. class SMPPClientFactory(ClientFactory):
  9.  
  10. protocol = SMPPClientProtocol
  11.  
  12. def __init__(self, config):
  13. self.config = config
  14.  
  15. def getConfig(self):
  16. return self.config
  17.  
  18. def clientConnectionFailed(self, connector, reason):
  19. print "clientConnectionFailed"
  20.  
  21. self.connectDeferred.errback(reason)
  22.  
  23. def clientConnectionLost(self, connector, reason):
  24. print "clientConnectionLost"
  25.  
  26. def connect(self):
  27. self.connectDeferred = defer.Deferred()
  28. factory = SMPPClientFactory(self.config, self.msgHandler)
  29.  
  30. self.log.warning('Establishing TCP connection to %s:%d' % (self.config.host, self.config.port))
  31. reactor.connectTCP(self.config.host, self.config.port, factory)
  32.  
  33. return self.connectDeferred
  34.  
  35. import logging, traceback
  36. from twisted.internet import reactor, defer
  37. from protocols.smpp.configs import SMPPClientConfig
  38. from protocols.smpp.smpp_operations import SMPPOperationFactory
  39. from testbed.client import SMPPClientFactory
  40.  
  41. class SMPP(object):
  42.  
  43. def __init__(self, config=None):
  44. if config is None:
  45. config = SMPPClientConfig()
  46.  
  47. self.config = config
  48. self.opFactory = SMPPOperationFactory(config)
  49.  
  50. def run(self):
  51. try:
  52. #Bind
  53. SMPPClientFactory(self.config, self.handleMsg).connect().addErrback(self.connectFailed)
  54. except Exception, e:
  55. print "ERROR: %s" % str(e)
  56.  
  57. def connectFailed(self, reason):
  58. print "Connection failed %s" % str(reason)
  59.  
  60. def handleMsg(self, smpp, pdu):
  61. pass
  62.  
  63. if __name__ == '__main__':
  64. config = SMPPClientConfig(host='127.0.0.1', port=2775, username='smppclient1', password='password',
  65. log_level=logging.DEBUG)
  66.  
  67. logging.basicConfig(level=config.log_level, filename=config.log_file, format=config.log_format,datefmt=config.log_dateformat)
  68. SMPP(config).run()
  69. reactor.run()
Add Comment
Please, Sign In to add comment