Twisted - listenTCP and connectTCP on the same reactor?

Andrew Bennetts andrew-pythonlist at puzzling.org
Sat Jun 14 22:00:17 EDT 2003


[While some Twisted developers do read comp.lang.python, you're usually
better off trying the twisted-python at twistedmatrix.com mailing list]

On Sat, Jun 14, 2003 at 08:01:19PM -0400, Ed Young wrote:
> 
> The question: I am working on an application to application
> data transporter.  It has to listen as well as connect.
> I've got the listenTCP working well.  I am having trouble
> getting the connectTCP to work.  I have a running reactor
> and I want to 'attach' a connectTCP to it.  How do I do
> this?

[...]

> class TransporterFactory(ClientFactory):
> 
>     def buildProtocol(self, addr):
>         return XmitProtocol()

[...]

> I am getting the error:
>   File "/home/ejy/src/py/aadm-twisted/aadm/aadmd/
>    XmitProtocol.py", line 33, in connectionMade
>     self.xmitq = self.factory.xmitq
> exceptions.AttributeError: XmitProtocol instance has 
> no attribute 'factory'
> 
> 
> This seems to be contradictory to what the docs say.
> Any guidance in this would be greatly appreciated...

This is contradictory to the docs, but only because your code explicitly
contradicts them ;)

A more idiomatic way to define your TransporterFactory would be:

    class TransporterFactory(ClientFactory):
        protocol = XmitProtocol

The problem is you overrode the default buildProtocol, which is:

    def buildProtocol(self, addr):
        """Create an instance of a subclass of Protocol.

        The returned instance will handle input on an incoming server
        connection, and an attribute \"factory\" pointing to the creating
        factory.

        Override this method to alter how Protocol instances get created.
        """
        p = self.protocol()
        p.factory = self
        return p

So you changed the behaviour; specifically, you didn't set a .factory
attribute on the protocol you built.

-Andrew.






More information about the Python-list mailing list