[Twisted-Python] attribute error when I am trying to make one factory to send data to another

Pantelis Theodosiou ypercube at gmail.com
Tue Apr 28 13:21:08 MDT 2015


That's what happens when one doesn't try running the code he suggests,
sorry. It should be self.factory.app... :

            for echoer in self.factory.app.multiechofactory.echoers:
                echoer.dataReceived(sendermessage)


On Tue, Apr 28, 2015 at 7:41 PM, Jessica Tsui <jesadjust at gmail.com> wrote:

> Pantellis thank you so much for your guide! I am still quite unfamiliar
> with python and twisted so I made all kind of silly mistakes and could not
> figure out how to fix them
>
> I tried your code, it seems to me that it should be working with by
> referencing the factories as well as the self.app...Unfortunately when I
> ran it it did not work well, the program has an error with your line
>
>             for echoer in self.app.multiechofactory.echoers:
>                 echoer.dataReceived(sendermessage)
>
> The error is: exceptions.AttributeError: MultiEcho instance has no
> attribute 'app'
>
> I thought that the "app" has already been referenced?
>
> 2015-04-29 1:55 GMT+08:00 Pantelis Theodosiou <ypercube at gmail.com>:
>
>>
>> On Tue, Apr 28, 2015 at 6:46 PM, Pantelis Theodosiou <ypercube at gmail.com>
>> wrote:
>>
>>> No, I don't think that would work.
>>>
>>> You would need something like changing these lines:
>>>
>>>         reactor.listenTCP(8000, EchoFactory(self))  # for sender
>>>         reactor.listenTCP(8001, MultiEchoFactory(self))  # for receiver
>>>
>>> to:
>>>
>>>         self.echofactory = EchoFactory(self)
>>>         self.multiechofactory = MultiEchoFactory(self)
>>>         reactor.listenTCP(8000, self.echofactory)  # for sender
>>>         reactor.listenTCP(8001, self.multiechofactory)  # for receiver
>>>
>>> so, your "app" object (which is just a normal Python object) knows the
>>> other two objects (the instances of the 2 factories).
>>>
>>> Then, your calls:
>>>
>>>         if sendermessage:
>>>             self.transport.write(data)
>>>             #this line here is the trouble maker that caused the error
>>>             MultiEcho().dataReceived(sendermessage)
>>>
>>> could be written:
>>>
>>>         if sendermessage:
>>>             self.transport.write(data)
>>>
>>>             self.app.multiechofactory.dataReceived(sendermessage)
>>>
>>
>>
>> Oh, I messed it up. The first part is good but the factories do not have
>> dataReceived methods.
>>
>> It would have to be - depending on what you want to do - say, you want to
>> echo to all echoers:
>>
>>         if sendermessage:
>>             self.transport.write(data)
>>
>>             for echoer in self.app.multiechofactory.echoers:
>>                 echoer.dataReceived(sendermessage)
>>
>> or:
>>
>>         if sendermessage:
>>             self.transport.write(data)
>>
>>             self.app.multiechofactory.echo_them_all()
>>
>> and define the echo_them_all() method in the MultiEchoFactory() class.
>>
>>
>>
>>>
>>> On Tue, Apr 28, 2015 at 6:28 PM, Jessica Tsui <jesadjust at gmail.com>
>>> wrote:
>>>
>>>> Hi Daniel,
>>>>
>>>>   Thank you so much for your suggestion. I am quite new to python and
>>>> twisted so I am not very certain about how to make it work well. Does that
>>>> mean if I change the dataReceived by adding a line to instantiate the
>>>> MultiEchoFactory like this, it will work?
>>>>
>>>> def dataReceived(self, data):
>>>>     "As soon as any data is received, write it back."
>>>>     handlesendermessage = self.factory.app.handle_message(data)
>>>>
>>>>     if handlesendermessage:
>>>>         self.transport.write(data)
>>>>         MultiEchoFactory()
>>>>         MultiEcho().dataReceived(data)
>>>>
>>>>
>>>> 2015-04-29 0:33 GMT+08:00 Louis D. Burr <ldanielburr at me.com>:
>>>>
>>>>> Hi Jessica,
>>>>>
>>>>> On Apr 28, 2015, at 10:14 AM, Jessica Tsui <jesadjust at gmail.com>
>>>>> wrote:
>>>>>
>>>>> SNIP
>>>>>
>>>>>  exceptions.AttributeError: MultiEcho instance has no attribute ‘factory'
>>>>>
>>>>> SNIP
>>>>>
>>>>> MultiEcho().dataReceived(sendermessage)
>>>>>
>>>>> Here you create an instance of the protocol directly, i.e., without
>>>>> having instantiated a MutilEchoFactory.  The factory’s buildProtocol method
>>>>> is what assigns the factory instance as a member of the protocol
>>>>> (self.factory), and since you didn’t create the protocol via a factory, you
>>>>> have no factory attribute on your protocol instance.
>>>>>
>>>>> There are a number of ways to fix this, but generally speaking, you
>>>>> need to provide a way for your factories and their protocols to be aware of
>>>>> each other.
>>>>>
>>>>> Hope this helps,
>>>>>
>>>>> Daniel
>>>>> --
>>>>> L. Daniel Burr
>>>>> ldanielburr at me.com
>>>>> (312) 656-8387
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> Twisted-Python mailing list
>>>>> Twisted-Python at twistedmatrix.com
>>>>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>>>>>
>>>>>
>>>>
>>>> _______________________________________________
>>>> Twisted-Python mailing list
>>>> Twisted-Python at twistedmatrix.com
>>>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>>>>
>>>>
>>>
>>
>> _______________________________________________
>> Twisted-Python mailing list
>> Twisted-Python at twistedmatrix.com
>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>>
>>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20150428/61cbc804/attachment-0002.html>


More information about the Twisted-Python mailing list