#NEWLINE# def handshake( self ) : #NEWLINE# #INDENT# self . log . debug ( 'handshake' ) #NEWLINE# data = self . request . recv ( 1024 ) . strip ( ) #NEWLINE# key = data . decode ( ) . split ( 'Sec-WebSocket-Key: ' ) [ 1 ] . split ( '\r\n' ) [ 0 ] #NEWLINE# digest = hashlib . sha1 ( ( key . encode ( 'utf-8' ) + self . magic ) ) #NEWLINE# digest = digest . digest ( ) #NEWLINE# digest = base64 . b64encode ( digest ) #NEWLINE# response = 'HTTP/1.1 101 Switching Protocols\r\n' #NEWLINE# response += 'Upgrade: websocket\r\n' #NEWLINE# response += 'Connection: Upgrade\r\n' #NEWLINE# response += ( 'Sec-WebSocket-Accept: %s\r\n\r\n' % digest . decode ( 'utf-8' ) ) #NEWLINE# self . log . info ( 'handshake complete' ) #NEWLINE# self . request . sendall ( response . encode ( 'utf-8' ) ) #NEWLINE# self . handshake_done = True