getting twisted to work in python - sending data from one protocol to another

jesadjust at gmail.com jesadjust at gmail.com
Tue Apr 28 15:02:01 CEST 2015


I am a newbie in python. Right now I am working on this messaging app and I would like to pass data from one protocol 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 it is only writing back to itself now instead of writing to another factory, and I cannot figure out why. If anyone can provide any sort of help I will really appreciate it as I have been stuck in this point for quite a while.

Here is the code

# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scatter import Scatter
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button
from kivy.graphics.vertex_instructions import Rectangle
from kivy.graphics.context_instructions import Color
from kivy.graphics.instructions import Instruction
from kivy.base import runTouchApp
from kivy.lang import Builder
import socket
from kivy.core.window import Window
import pygame
import random
from kivy.support import install_twisted_reactor

install_twisted_reactor()
from twisted.internet import reactor, protocol

Window.size = (550, 400)

# monitoring wordlist
with open("wordlist.txt") as word_file:
    wordlist = list(word.strip().lower() for word in word_file)

# protocols for sender
class 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 = data
        if sendermessage:
            self.transport.write(sendermessage)
            MultiEcho().dataReceived(self.transport.write("sender: %s" %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 = data
        # handlereceivermessage = self.factory.app.handle_message(data)
        if receivermessage:
            self.transport.write(receivermessage)
            EchoProtocol().dataReceived(self.transport.write("receiver: %s" %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, MultiEchoFactory(self))  # for sender
        reactor.listenTCP(8001, EchoFactory(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()



More information about the Python-list mailing list