#NEWLINE# def _sendMessage( self , fin , opcode , data ) : #NEWLINE# #INDENT# header = bytearray ( ) #NEWLINE# b1 = 0 #NEWLINE# b2 = 0 #NEWLINE# if ( fin is False ) : #NEWLINE# #INDENT# b1 |= 128 #NEWLINE# #UNINDENT# b1 |= opcode #NEWLINE# if isinstance ( data , unicode ) : #NEWLINE# #INDENT# data = data . encode ( 'utf-8' ) #NEWLINE# #UNINDENT# length = len ( data ) #NEWLINE# header . append ( b1 ) #NEWLINE# if ( length <= 125 ) : #NEWLINE# #INDENT# b2 |= length #NEWLINE# header . append ( b2 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( length >= 126 ) and ( length <= 65535 ) ) : #NEWLINE# #INDENT# b2 |= 126 #NEWLINE# header . append ( b2 ) #NEWLINE# header . extend ( struct . pack ( '!H' , length ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# b2 |= 127 #NEWLINE# header . append ( b2 ) #NEWLINE# header . extend ( struct . pack ( '!Q' , length ) ) #NEWLINE# #UNINDENT# #UNINDENT# payload = None #NEWLINE# if ( length > 0 ) : #NEWLINE# #INDENT# payload = ( str ( header ) + str ( data ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# payload = str ( header ) #NEWLINE# #UNINDENT# self . sendq . append ( ( opcode , payload ) )