<s> import hashlib <EOL> from ranstring import randomByteString <EOL> from twisted . internet import reactor <EOL> from autobahn . twisted . websocket import WebSocketClientFactory , WebSocketClientProtocol , connectWS <EOL> FRAME_SIZE = <NUM_LIT:1> * <NUM_LIT:2> ** <NUM_LIT:20> <EOL> FRAME_COUNT = <NUM_LIT:10> <EOL> class FrameBasedHashClientProtocol ( WebSocketClientProtocol ) : <EOL> """<STR_LIT>""" <EOL> def sendOneFrame ( self ) : <EOL> data = randomByteString ( FRAME_SIZE ) <EOL> self . sha256 . update ( data ) <EOL> digest = self . sha256 . hexdigest ( ) <EOL> print ( "<STR_LIT>" . format ( self . count , digest ) ) <EOL> self . sendMessageFrame ( data ) <EOL> def onOpen ( self ) : <EOL> self . count = <NUM_LIT:0> <EOL> self . finished = False <EOL> self . beginMessage ( isBinary = True ) <EOL> self . sha256 = hashlib . sha256 ( ) <EOL> self . sendOneFrame ( ) <EOL> def onMessage ( self , payload , isBinary ) : <EOL> print ( "<STR_LIT>" . format ( self . count , payload . decode ( '<STR_LIT:utf8>' ) ) ) <EOL> self . count += <NUM_LIT:1> <EOL> if self . count < FRAME_COUNT : <EOL> self . sendOneFrame ( ) <EOL> elif not self . finished : <EOL> self . endMessage ( ) <EOL> self . finished = True <EOL> if self . count >= FRAME_COUNT : <EOL> self . sendClose ( ) <EOL> def onClose ( self , wasClean , code , reason ) : <EOL> reactor . stop ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> factory = WebSocketClientFactory ( u"<STR_LIT>" ) <EOL> factory . protocol = FrameBasedHashClientProtocol <EOL> enableCompression = False <EOL> if enableCompression : <EOL> from autobahn . websocket . compress import PerMessageDeflateOffer , PerMessageDeflateResponse , PerMessageDeflateResponseAccept <EOL> offers = [ PerMessageDeflateOffer ( ) ] <EOL> factory . setProtocolOptions ( perMessageCompressionOffers = offers ) <EOL> def accept ( response ) : <EOL> if isinstance ( response , PerMessageDeflateResponse ) : <EOL> return PerMessageDeflateResponseAccept ( response ) <EOL> factory . setProtocolOptions ( perMessageCompressionAccept = accept ) <EOL> connectWS ( factory ) <EOL> reactor . run ( ) </s>
