[Twisted-Python] Not understanding buildProtocol(), any clarification?

John Crawford cyclops at speakeasy.net
Wed Sep 30 16:16:48 EDT 2009

Hello, I'm trying to understand and use buildProtocol(), which isn't 
working as I expected, in two ways :) Question 1. If I have, on the 
Server side:

class MyFactory(Factory):
         protocol = MyTestProtocol
         def buildProtocol(self, addr):
                 p = Factory.buildProtocol(self, addr)
                 return p

Should this work? Would the write() function execute? I thought that 
'p' was a fully usable Protocol object at that point, but Python 
disagrees with me :) Tells me it's a 'Nonetype' with no write() 
method. If I take out the write(), everything works fine, I can 
communicate between client/server, so no problems in the rest of the code.

Question 2. If I use buildProtocol() on the Server side to *not* make 
a connection, as:

class MyFactory(Factory):
         protocol = MyTestProtocol
         def buildProtocol(self, addr):
                 if (some condition):
                         return Factory.buildProtocol(self, addr)
                         return None

What is supposed to happen on the Client Side when I return None? I 
was thinking that my factory's clientConnectionFailed() would fire, 
indicating that the Server refused a connection.

Instead, what I get is my Client's connectionMade() firing, 
immediately followed by a clientConnectionLost() firing, as if the 
connection was made then dropped.

Just to check, I changed it to:
         buildProtocol(self, addr):
                 return None

which behaved the same way on the Client side. So what is really 
happening in buildProtocol? Is it making a connection to the Client 
side, and firing connectionMade(), no matter what? Or is something 
else going that I don't understand :)

Thanks in advance for any clarification.

John C>

More information about the Twisted-Python mailing list