[Twisted-Python] TCP client that can connect to a server at periodic intervals

I am trying to write a TCP client that at regular intervals connects to a server makes a request and then closes the connection. Below is the code I've been playing with. I've seen an example of LoopingCall used for periodically sending UPD packets but am not sure how it would apply here because it would seem I need to periodically create new Protocol instances in the ClientFactory class. I've looked for examples and through the documentation but have come up empty. Any pointers would be greatly appreceiated. class AnnouncementFetcher(protocol.Protocol): def connectionMade(self): print "connectionMade called" self.transport.write(MSG_INFO_IPC) def dataReceived(self, data): print "Received: ", repr(data) # self.factory.share.saveData(data) self.transport.loseConnection() class AnnouncementFetcherFactory(protocol.ClientFactory): protocol = AnnouncementFetcher def __init__(self, share): self.share = share def buildProtocol(self, addr): print 'Connected.' p = AnnouncementFetcher() p.factory = self return p def clientConnectionLost(self, connector, reason): print 'Lost connection, Reason:', reason reactor.callLater(5, self.buildProtocol, 'localhost') def clientConnectionFailed(self, connector, reason): print 'Connection failed. Reason:', reason

On 05:58 pm, bradley.s.gaspard@saic.com wrote:
I am trying to write a TCP client that at regular intervals connects to a server makes a request and then closes the connection.
Your code has a fatal problem. Nowhere are you actually creating a client connection, you're just creating protocol objects and not hooking them up to anything! I've whipped up a quick periodic ping/pong client/server pair for you here, which should provide a useful starting point for you. The key thing is the call to the ClientCreator's 'connectTCP' method, which actually hooks up an instantiated PingSender to a socket. -------- cut from twisted.internet.protocol import Protocol, ClientCreator, Factory from twisted.internet.task import LoopingCall from twisted.internet import reactor class PingResponder(Protocol): buf = '' def dataReceived(self, data): self.buf += data if self.buf == 'PING': print 'PONGED!' self.transport.write('PONG') self.transport.loseConnection() class PingSender(Protocol): buf = '' def connectionMade(self): self.transport.write("PING") def dataReceived(self, data): self.buf += data def connectionLost(self, reason): print "PONGED WITH: " +self.buf def client(): cc = ClientCreator(reactor, PingSender) def dontDelay(): cc.connectTCP('localhost', 4321) lc = LoopingCall(dontDelay) lc.start(0.5) def server(): pf = Factory() pf.protocol = PingResponder svr = reactor.listenTCP(4321, pf) import sys if sys.argv[1] == 'client': client() else: server() reactor.run()

Hello, I will likely get to work on a gateway between SOAP-based Web Services and SIP-based services. I would like to explore the possibility of implementing it using the Twisted framework and I'd appreciate if you can give me suggestions on: 1. Which SOAP stack to use. I've heard that ZopeLib is the way to go these days. 2. Which SIP stack to use. I will eventually fire questions about the overall architecture the gateway software should have. Any hints here are welcome. Thank you, -- Pedro

Hi Pedro On 3/15/07, Pedro Sanchez <psanchez@nortel.com> wrote:
Hello,
I will likely get to work on a gateway between SOAP-based Web Services and SIP-based services.
This sounds slightly strange in my ears, but thats probably just me. I would like to explore the possibility of
implementing it using the Twisted framework and I'd appreciate if you can give me suggestions on:
1. Which SOAP stack to use. I've heard that ZopeLib is the way to go these days.
You probably want ZSI: http://pywebsvcs.sourceforge.net/ I know it has some twisted web resource that might be usefull. I don't think there is any actual software called ZopeLib. SOAP and Zope has similar phonetic sounds, so there might a misunderstanding here. 2. Which SIP stack to use. There is twisted/protocols/sip.py. There is Shtoom ( http://divmod.org/trac/wiki/ShtoomProject) if you need voice over IP.
I will eventually fire questions about the overall architecture the gateway software should have. Any hints here are welcome.
Start by understanding each of the above things before combining them :-) -- - Henrik

On Thu, 15 Mar 2007 17:34:59 +0100, Henrik Thostrup Jensen <thostrup@gmail.com> wrote:
Hi Pedro
On 3/15/07, Pedro Sanchez <psanchez@nortel.com> wrote:
Hello,
I will likely get to work on a gateway between SOAP-based Web Services and SIP-based services.
This sounds slightly strange in my ears, but thats probably just me.
I would like to explore the possibility of
implementing it using the Twisted framework and I'd appreciate if you can give me suggestions on:
1. Which SOAP stack to use. I've heard that ZopeLib is the way to go these days.
You probably want ZSI: http://pywebsvcs.sourceforge.net/
I know it has some twisted web resource that might be usefull.
I don't think there is any actual software called ZopeLib. SOAP and Zope has similar phonetic sounds, so there might a misunderstanding here.
2. Which SIP stack to use.
There is twisted/protocols/sip.py. There is Shtoom ( http://divmod.org/trac/wiki/ShtoomProject) if you need voice over IP.
Slight correction. Rather than twisted/protocols/sip.py, you probably want to use the protocol code from Divmod Sine. There is some effort to backport improvements and fixes to Twisted, but this is still in progress, so you'll be missing out on a ton of stuff if you use what's in any existing Twisted release.
I will eventually fire questions about the overall architecture the gateway software should have. Any hints here are welcome.
Start by understanding each of the above things before combining them :-)
Jean-Paul

On Thu, 2007-03-15 at 12:29 -0500, Jean-Paul Calderone wrote:
On Thu, 15 Mar 2007 17:34:59 +0100, Henrik Thostrup Jensen <thostrup@gmail.com> wrote:
Hi Pedro
On 3/15/07, Pedro Sanchez <psanchez@nortel.com> wrote:
Hello,
I will likely get to work on a gateway between SOAP-based Web Services and SIP-based services.
This sounds slightly strange in my ears, but thats probably just me.
I would like to explore the possibility of
implementing it using the Twisted framework and I'd appreciate if you can give me suggestions on:
1. Which SOAP stack to use. I've heard that ZopeLib is the way to go these days.
You probably want ZSI: http://pywebsvcs.sourceforge.net/
I know it has some twisted web resource that might be usefull.
I don't think there is any actual software called ZopeLib. SOAP and Zope has similar phonetic sounds, so there might a misunderstanding here.
OK, the recommendation I'm getting from a colleague is SOAPlib (sorry for the confusion) from http://trac.optio.webfactional.com/ Any experience with this one?
2. Which SIP stack to use.
There is twisted/protocols/sip.py. There is Shtoom ( http://divmod.org/trac/wiki/ShtoomProject) if you need voice over IP.
Slight correction. Rather than twisted/protocols/sip.py, you probably want to use the protocol code from Divmod Sine. There is some effort to backport improvements and fixes to Twisted, but this is still in progress, so you'll be missing out on a ton of stuff if you use what's in any existing Twisted release.
Thanks, I'll take a look at Sine.
I will eventually fire questions about the overall architecture the gateway software should have. Any hints here are welcome.
Start by understanding each of the above things before combining them :-)
Jean-Paul
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python -- Pedro I. Sanchez Rapid Prototyping Lab CTO, Nortel ESN 393-6084 External 613-763-6084
participants (5)
-
Gaspard, Bradley S
-
glyph@divmod.com
-
Henrik Thostrup Jensen
-
Jean-Paul Calderone
-
Pedro Sanchez