<s> import sys <EOL> from twisted . internet import reactor <EOL> from twisted . python import log <EOL> from autobahn . twisted . websocket import WebSocketClientFactory , WebSocketClientProtocol , connectWS <EOL> class EchoClientProtocol ( WebSocketClientProtocol ) : <EOL> def sendHello ( self ) : <EOL> self . sendMessage ( "<STR_LIT>" . encode ( '<STR_LIT:utf8>' ) ) <EOL> def onOpen ( self ) : <EOL> self . sendHello ( ) <EOL> def onClose ( self , wasClean , code , reason ) : <EOL> print ( reason ) <EOL> def onMessage ( self , payload , isBinary ) : <EOL> if not isBinary : <EOL> print ( "<STR_LIT>" . format ( payload . decode ( '<STR_LIT:utf8>' ) ) ) <EOL> reactor . callLater ( <NUM_LIT:1> , self . sendHello ) <EOL> class EchoClientFactory ( WebSocketClientFactory ) : <EOL> protocol = EchoClientProtocol <EOL> def clientConnectionLost ( self , connector , reason ) : <EOL> print ( reason ) <EOL> reactor . stop ( ) <EOL> def clientConnectionFailed ( self , connector , reason ) : <EOL> print ( reason ) <EOL> reactor . stop ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if len ( sys . argv ) < <NUM_LIT:2> : <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> factory = EchoClientFactory ( sys . argv [ <NUM_LIT:1> ] ) <EOL> connectWS ( factory ) <EOL> reactor . run ( ) </s>
