<s> from kivy . support import install_twisted_reactor <EOL> install_twisted_reactor ( ) <EOL> from twisted . internet import reactor , protocol <EOL> class EchoClient ( protocol . Protocol ) : <EOL> def connectionMade ( self ) : <EOL> self . factory . app . on_connection ( self . transport ) <EOL> def dataReceived ( self , data ) : <EOL> self . factory . app . print_message ( data ) <EOL> class EchoFactory ( protocol . ClientFactory ) : <EOL> protocol = EchoClient <EOL> def __init__ ( self , app ) : <EOL> self . app = app <EOL> def clientConnectionLost ( self , conn , reason ) : <EOL> self . app . print_message ( "<STR_LIT>" ) <EOL> def clientConnectionFailed ( self , conn , reason ) : <EOL> self . app . print_message ( "<STR_LIT>" ) <EOL> from kivy . app import App <EOL> from kivy . uix . label import Label <EOL> from kivy . uix . textinput import TextInput <EOL> from kivy . uix . boxlayout import BoxLayout <EOL> class TwistedClientApp ( App ) : <EOL> connection = None <EOL> def build ( self ) : <EOL> root = self . setup_gui ( ) <EOL> self . connect_to_server ( ) <EOL> return root <EOL> def setup_gui ( self ) : <EOL> self . textbox = TextInput ( size_hint_y = <NUM_LIT> , multiline = False ) <EOL> self . textbox . bind ( on_text_validate = self . send_message ) <EOL> self . label = Label ( text = '<STR_LIT>' ) <EOL> self . layout = BoxLayout ( orientation = '<STR_LIT>' ) <EOL> self . layout . add_widget ( self . label ) <EOL> self . layout . add_widget ( self . textbox ) <EOL> return self . layout <EOL> def connect_to_server ( self ) : <EOL> reactor . connectTCP ( '<STR_LIT:localhost>' , <NUM_LIT> , EchoFactory ( self ) ) <EOL> def on_connection ( self , connection ) : <EOL> self . print_message ( "<STR_LIT>" ) <EOL> self . connection = connection <EOL> def send_message ( self , * args ) : <EOL> msg = self . textbox . text <EOL> if msg and self . connection : <EOL> self . connection . write ( str ( self . textbox . text ) ) <EOL> self . textbox . text = "<STR_LIT>" <EOL> def print_message ( self , msg ) : <EOL> self . label . text += msg + "<STR_LIT:\n>" <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> TwistedClientApp ( ) . run ( ) </s>
