<s> from autobahn . twisted . websocket import WebSocketClientProtocol , WebSocketClientFactory <EOL> from twisted . internet . defer import Deferred , inlineCallbacks <EOL> def sleep ( delay ) : <EOL> d = Deferred ( ) <EOL> reactor . callLater ( delay , d . callback , None ) <EOL> return d <EOL> class MyClientProtocol ( WebSocketClientProtocol ) : <EOL> def onConnect ( self , response ) : <EOL> print ( "<STR_LIT>" . format ( response . peer ) ) <EOL> @ inlineCallbacks <EOL> def onOpen ( self ) : <EOL> print ( "<STR_LIT>" ) <EOL> while True : <EOL> self . sendMessage ( u"<STR_LIT>" . encode ( '<STR_LIT:utf8>' ) ) <EOL> self . sendMessage ( b"<STR_LIT>" , isBinary = True ) <EOL> yield sleep ( <NUM_LIT:1> ) <EOL> def onMessage ( self , payload , isBinary ) : <EOL> if isBinary : <EOL> print ( "<STR_LIT>" . format ( len ( payload ) ) ) <EOL> else : <EOL> print ( "<STR_LIT>" . format ( payload . decode ( '<STR_LIT:utf8>' ) ) ) <EOL> def onClose ( self , wasClean , code , reason ) : <EOL> print ( "<STR_LIT>" . format ( reason ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> from twisted . python import log <EOL> from twisted . internet import reactor <EOL> log . startLogging ( sys . stdout ) <EOL> factory = WebSocketClientFactory ( u"<STR_LIT>" , debug = False ) <EOL> factory . protocol = MyClientProtocol <EOL> reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> , factory ) <EOL> reactor . run ( ) </s>
