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