<s> import txaio <EOL> txaio . use_asyncio ( ) <EOL> import autobahn <EOL> from autobahn . asyncio . websocket import WebSocketClientProtocol , WebSocketClientFactory <EOL> from autobahn . websocket . compress import PerMessageDeflateOffer , PerMessageDeflateResponse , PerMessageDeflateResponseAccept <EOL> class TesteeClientProtocol ( WebSocketClientProtocol ) : <EOL> def onOpen ( self ) : <EOL> if self . factory . endCaseId is None : <EOL> self . log . info ( "<STR_LIT>" ) <EOL> elif self . factory . currentCaseId <= self . factory . endCaseId : <EOL> self . log . info ( "<STR_LIT>" , <EOL> case_id = self . factory . currentCaseId , <EOL> last_case_id = self . factory . endCaseId , <EOL> agent = self . factory . agent , <EOL> peer = self . peer ) <EOL> def onMessage ( self , msg , binary ) : <EOL> if self . factory . endCaseId is None : <EOL> self . factory . endCaseId = int ( msg ) <EOL> self . log . info ( "<STR_LIT>" , case_count = self . factory . endCaseId ) <EOL> else : <EOL> self . sendMessage ( msg , binary ) <EOL> class TesteeClientFactory ( WebSocketClientFactory ) : <EOL> protocol = TesteeClientProtocol <EOL> def __init__ ( self , url ) : <EOL> self . agent = autobahn . asyncio . __ident__ <EOL> WebSocketClientFactory . __init__ ( self , url , useragent = self . agent ) <EOL> self . setProtocolOptions ( failByDrop = False ) <EOL> offers = [ PerMessageDeflateOffer ( ) ] <EOL> self . setProtocolOptions ( perMessageCompressionOffers = offers ) <EOL> def accept ( response ) : <EOL> if isinstance ( response , PerMessageDeflateResponse ) : <EOL> return PerMessageDeflateResponseAccept ( response ) <EOL> self . setProtocolOptions ( perMessageCompressionAccept = accept ) <EOL> self . endCaseId = None <EOL> self . currentCaseId = <NUM_LIT:0> <EOL> self . updateReports = True <EOL> self . resource = "<STR_LIT>" <EOL> def clientConnectionLost ( self , connector , reason ) : <EOL> self . currentCaseId += <NUM_LIT:1> <EOL> if self . currentCaseId <= self . endCaseId : <EOL> self . resource = "<STR_LIT>" . format ( self . currentCaseId , self . agent ) <EOL> connector . connect ( ) <EOL> elif self . updateReports : <EOL> self . resource = "<STR_LIT>" . format ( self . agent ) <EOL> self . updateReports = False <EOL> connector . connect ( ) <EOL> else : <EOL> reactor . stop ( ) <EOL> def clientConnectionFailed ( self , connector , reason ) : <EOL> self . log . info ( "<STR_LIT>" , url = self . url , error_message = reason . getErrorMessage ( ) ) <EOL> reactor . stop ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> txaio . start_logging ( level = '<STR_LIT:info>' ) <EOL> try : <EOL> import asyncio <EOL> except ImportError : <EOL> import trollius as asyncio <EOL> factory = TesteeClientFactory ( u"<STR_LIT>" ) <EOL> loop = asyncio . get_event_loop ( ) <EOL> coro = loop . create_connection ( factory , '<STR_LIT:127.0.0.1>' , <NUM_LIT> ) <EOL> loop . run_until_complete ( coro ) <EOL> loop . run_forever ( ) <EOL> loop . close ( ) </s>
