<br><br>Hello everyone. Try as I might I cannot get a very simple example to
run. I basically want a server that accepts connections, and
periodically issues messages to all clients. The docs for twisted.word
says things are in flux so I would rather avoid that and just use a
LineReceiver. However right now I can't get anything to work.<br>
<br>I start the server and it seems to be happy to sit around and
accept connections. However when I run the client it quits (without
saying why) and no connection is made. Any help would be very much
appreciated. Here's the code, log messages below that. Thanks!<br>
<br> Kevin<br><br>##############################<div id="1euf" class="ArwC7c ckChnd">####################<br># server.py<br>##################################################<br>
import sys <br>from twisted.internet.protocol import log<br>from twisted.internet.protocol import Factory<br>from twisted.internet.protocol import Protocol<br>from twisted.internet import reactor<br><br>class Echo(Protocol):<br>
<br> def connectionMade(self):<br> sys.stderr.write('Server connectionMade\n')<br> self.factory.numProtocols = self.factory.numProtocols+1<br> if self.factory.numProtocols > 100:<br>
self.transport.write("Too many connections, try later")<br> self.transport.loseConnection()<br><br> def connectionLost(self, reason):<br> sys.stderr.write('Server connectionLost\n')<br>
self.factory.numProtocols = self.factory.numProtocols-1<br><br> def dataReceived(self, data):<br> sys.stderr.write('Server dataReceived\n')<br> self.transport.write(data)<br><br><br>factory = Factory()<br>
factory.protocol = Echo<br><br>log.startLogging(file('server.log', 'w'))<br><br>sys.stderr.write('Server starting...\n')<br># 8007 is the port you want to run under. Choose something >1024<br>reactor.listenTCP(9010, factory)<br>
reactor.run()<br>sys.stderr.write('Server done\n')<br><br><br><br><br><br>##################################################<br>
# client.py<br>
##################################################<br>
import sys <br>from twisted.internet import reactor<br>from twisted.internet.protocol import log<br>from twisted.internet.protocol import Protocol<br>from twisted.internet.protocol import ClientCreator<br>from twisted.internet.protocol import ClientFactory<br>
<br>class Greeter(Protocol):<br> def sendMessage(self, msg):<br> sys.stderr.write('Client sendMessage\n')<br> self.transport.write("MESSAGE %s\n" % msg)<br> <br> <br>class GreeterClientFactory(ClientFactory):<br>
def startedConnecting(self, connector):<br> sys.stderr.write('Client startedConnecting\n')<br> <br> def buildProtocol(self, addr):<br> sys.stderr.write('Client connected\n')<br>
g = Greeter()<br> reactor.callLater(1, g.sendMessage, "This is sent in 1 second")<br> reactor.callLater(2, g.sendMessage, "This is sent in 2 second")<br> reactor.callLater(3, g.sendMessage, "This is sent in 3 second")<br>
return Greeter()<br> <br> def clientConnectionLost(self, connector, reason):<br> sys.stderr.write('Client connectionLost: ' + reason + '\n')<br> <br> def clientConnectionFailed(self, connector, reason):<br>
sys.stderr.write('Client connectionFailed: ' + reason + '\n')<br><br><br>log.startLogging(file('client.log', 'w'))<br><br>sys.stderr.write('Client connecting...\n')<br><br>
f = GreeterClientFactory()<br>
reactor.connectTCP("localhost", 9010, f)<br><br>sys.stderr.write('Client done\n')<br><br><br><br><br>##################################################<br>
# client.log<br>##################################################<br>
2008/04/28 12:38 -0400 [-] Log opened.<br>2008/04/28 12:38 -0400 [-] Client connecting...<br>2008/04/28 12:38 -0400 [-] Starting factory <__main__.GreeterClientFactory instance at 0x8354d8c><br>2008/04/28 12:38 -0400 [-] Client startedConnecting<br>
2008/04/28 12:38 -0400 [-] Client done<br><br><br><br><br><br>##################################################<br>
# server.log<br>
##################################################<br>2008/04/28 14:41 -0400 [-] Log opened.<br>2008/04/28 14:41 -0400 [-] Server starting...<br>2008/04/28 14:41 -0400 [-] twisted.internet.protocol.Factory starting on 9010<br>
2008/04/28 14:41 -0400 [-] Starting factory <twisted.internet.protocol.Factory instance at 0xb7cc18cc><br><br></div>