[Twisted-Python] attribute error when I am trying to make one factory to send data to another
Jessica Tsui
jesadjust at gmail.com
Tue Apr 28 09:14:36 MDT 2015
I am working on this messaging app and I would like to pass data from one
factory to another. I have been referencing to other answers provided in
other similar questions by trying to add a variable storing the data from
one factory then initiate another factory and write the data to it.
Unfortunately there is an error when i call the other class, and I cannot
figure out why. Is there anything I can do to fix it? I have been stuck in
here for quite a while
this is the error:
receivermessage = self.factory.app.handle_message2(data)
exceptions.AttributeError: MultiEcho instance has no attribute 'factory'
Here is the code:
import kivyfrom kivy.app import Appfrom kivy.uix.label import
Labelfrom kivy.uix.scatter import Scatterfrom kivy.uix.boxlayout
import BoxLayoutfrom kivy.uix.scrollview import ScrollViewfrom
kivy.uix.button import Buttonfrom kivy.graphics.vertex_instructions
import Rectanglefrom kivy.graphics.context_instructions import
Colorfrom kivy.graphics.instructions import Instructionfrom kivy.base
import runTouchAppfrom kivy.lang import Builderimport socketfrom
kivy.core.window import Windowimport pygameimport randomfrom
kivy.support import install_twisted_reactor
install_twisted_reactor()from twisted.internet import reactor, protocol
Window.size = (550, 400)
# monitoring wordlistwith open("wordlist.txt") as word_file:
wordlist = list(word.strip().lower() for word in word_file)
# protocols for senderclass EchoProtocol(protocol.Protocol):
"""This is just about the simplest possible protocol"""
def dataReceived(self, data):
"As soon as any data is received, write it back."
sendermessage = self.factory.app.handle_message(data)
if sendermessage:
self.transport.write(data)
#this line here is the trouble maker that caused the error
MultiEcho().dataReceived(sendermessage) class EchoFactory(protocol.Factory):
protocol = EchoProtocol def __init__(self, app): self.app = app # protocols
for receiver class MultiEcho(protocol.Protocol): def connectionMade(self):
self.factory.echoers.append(self) def dataReceived(self, data):
receivermessage = self.factory.app.handle_message2(data) if receivermessage:
self.transport.write(data)
#this line here is the trouble maker that caused the error
EchoProtocol().dataReceived(receivermessage)
class MultiEchoFactory(protocol.Factory):
protocol = MultiEcho
def __init__(self, app):
self.echoers = []
self.app = app
class ServerApp(App):
def build(self):
self.layout = BoxLayout(orientation='vertical', spacing=10)
self.label = Button(text='Censoring process begin\nBeware of
keyword "umbrella"\n ', color=[1.0, 1.0, 1.0, 1.0])
self.label.color = [0.9, 0.2, 0.2, 1.0]
self.upperscroll = Button(pos_hint={'x': 0, 'center_y': .5},
size_hint=(None, None))
self.scatter = Scatter()
self.displaybox = Label()
self.displaybox.color = [0.4, 0.9, 0.4, 1.0]
reactor.listenTCP(8000, EchoFactory(self)) # for sender
reactor.listenTCP(8001, MultiEchoFactory(self)) # for receiver
self.layout.add_widget(self.label)
self.layout.add_widget(self.scatter)
self.scatter.add_widget(self.displaybox)
return self.layout
def handle_message(self, msg):
if any(word in msg.lower() for word in wordlist):
self.displaybox.color = [0.9, 0.4, 0.4, 1.0]
self.displaybox.text = "content blocked"
self.label.text += "Alert! Sender posts %s \n" % msg
else:
self.label.text += "Safe - sender posts %s \n" % msg
self.displaybox.color = [0.4, 0.9, 0.4, 1.0]
self.displaybox.text = "%s" % msg
msg = msg
return msg
def handle_message2(self, msg):
if any(word in msg.lower() for word in wordlist):
self.label.color = [0.8, 0.8, 0.5, 1.0]
self.label.text += "Alert! Receiver got %s \n" % msg
else:
self.label.color = [0.2, 0.2, 1.0, 1.0]
self.label.text += "Safe - receiver sends %s \n" % msg
msg = msg
return msg
if __name__ == '__main__':
ServerApp().run()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-python/attachments/20150428/832941fd/attachment-0001.html>
More information about the Twisted-Python
mailing list