Update Twisted framework integration example to py3

- With maintaining support to Py2
This commit is contained in:
Manthan Sharma 2017-02-28 03:03:28 +05:30
parent c35fcca00f
commit fbc75199f7
2 changed files with 37 additions and 27 deletions

View File

@ -1,9 +1,11 @@
# install_twisted_rector must be called before importing the reactor
from __future__ import unicode_literals
from kivy.support import install_twisted_reactor
install_twisted_reactor()
# A simple Client that send messages to the echo server
# A Simple Client that send messages to the Echo Server
from twisted.internet import reactor, protocol
@ -12,20 +14,23 @@ class EchoClient(protocol.Protocol):
self.factory.app.on_connection(self.transport)
def dataReceived(self, data):
self.factory.app.print_message(data)
self.factory.app.print_message(data.decode('utf-8'))
class EchoFactory(protocol.ClientFactory):
class EchoClientFactory(protocol.ClientFactory):
protocol = EchoClient
def __init__(self, app):
self.app = app
def clientConnectionLost(self, conn, reason):
self.app.print_message("connection lost")
def startedConnecting(self, connector):
self.app.print_message('Started to connect.')
def clientConnectionFailed(self, conn, reason):
self.app.print_message("connection failed")
def clientConnectionLost(self, connector, reason):
self.app.print_message('Lost connection.')
def clientConnectionFailed(self, connector, reason):
self.app.print_message('Connection failed.')
from kivy.app import App
@ -39,6 +44,8 @@ from kivy.uix.boxlayout import BoxLayout
# the server
class TwistedClientApp(App):
connection = None
textbox = None
label = None
def build(self):
root = self.setup_gui()
@ -49,26 +56,26 @@ class TwistedClientApp(App):
self.textbox = TextInput(size_hint_y=.1, multiline=False)
self.textbox.bind(on_text_validate=self.send_message)
self.label = Label(text='connecting...\n')
self.layout = BoxLayout(orientation='vertical')
self.layout.add_widget(self.label)
self.layout.add_widget(self.textbox)
return self.layout
layout = BoxLayout(orientation='vertical')
layout.add_widget(self.label)
layout.add_widget(self.textbox)
return layout
def connect_to_server(self):
reactor.connectTCP('localhost', 8000, EchoFactory(self))
reactor.connectTCP('localhost', 8000, EchoClientFactory(self))
def on_connection(self, connection):
self.print_message("connected successfully!")
self.print_message("Connected successfully!")
self.connection = connection
def send_message(self, *args):
msg = self.textbox.text
if msg and self.connection:
self.connection.write(str(self.textbox.text))
self.connection.write(msg.encode('utf-8'))
self.textbox.text = ""
def print_message(self, msg):
self.label.text += msg + "\n"
self.label.text += "{}\n".format(msg)
if __name__ == '__main__':

View File

@ -1,21 +1,21 @@
# install_twisted_rector must be called before importing and using the reactor
from kivy.support import install_twisted_reactor
install_twisted_reactor()
install_twisted_reactor()
from twisted.internet import reactor
from twisted.internet import protocol
class EchoProtocol(protocol.Protocol):
class EchoServer(protocol.Protocol):
def dataReceived(self, data):
response = self.factory.app.handle_message(data)
if response:
self.transport.write(response)
class EchoFactory(protocol.Factory):
protocol = EchoProtocol
class EchoServerFactory(protocol.Factory):
protocol = EchoServer
def __init__(self, app):
self.app = app
@ -26,20 +26,23 @@ from kivy.uix.label import Label
class TwistedServerApp(App):
label = None
def build(self):
self.label = Label(text="server started\n")
reactor.listenTCP(8000, EchoFactory(self))
reactor.listenTCP(8000, EchoServerFactory(self))
return self.label
def handle_message(self, msg):
self.label.text = "received: %s\n" % msg
msg = msg.decode('utf-8')
self.label.text = "received: {}\n".format(msg)
if msg == "ping":
msg = "pong"
msg = "Pong"
if msg == "plop":
msg = "kivy rocks"
self.label.text += "responded: %s\n" % msg
return msg
msg = "Kivy Rocks!!!"
self.label.text += "responded: {}\n".format(msg)
return msg.encode('utf-8')
if __name__ == '__main__':