#NEWLINE# def upgrade_websocket( self , environ , start_response ) : #NEWLINE# #INDENT# "\n        Attempt to upgrade the socket environ['wsgi.input'] into a websocket enabled connection.\n        " #NEWLINE# websocket_version = environ . get ( 'HTTP_SEC_WEBSOCKET_VERSION' , '' ) #NEWLINE# if ( not websocket_version ) : #NEWLINE# #INDENT# raise UpgradeRequiredError #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( websocket_version not in self . WS_VERSIONS ) : #NEWLINE# #INDENT# raise HandshakeError ( 'Unsupported WebSocket Version: {0}' . format ( websocket_version ) ) #NEWLINE# #UNINDENT# #UNINDENT# key = environ . get ( 'HTTP_SEC_WEBSOCKET_KEY' , '' ) . strip ( ) #NEWLINE# if ( not key ) : #NEWLINE# #INDENT# raise HandshakeError ( 'Sec-WebSocket-Key header is missing/empty' ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# key_len = len ( base64 . b64decode ( key ) ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# raise HandshakeError ( 'Invalid key: {0}' . format ( key ) ) #NEWLINE# #UNINDENT# if ( key_len != 16 ) : #NEWLINE# #INDENT# raise HandshakeError ( 'Invalid key: {0}' . format ( key ) ) #NEWLINE# #UNINDENT# sec_ws_accept = base64 . b64encode ( sha1 ( ( six . b ( key ) + self . WS_GUID ) ) . digest ( ) ) #NEWLINE# if six . PY3 : #NEWLINE# #INDENT# sec_ws_accept = sec_ws_accept . decode ( 'ascii' ) #NEWLINE# #UNINDENT# headers = [ ( 'Upgrade' , 'websocket' ) , ( 'Connection' , 'Upgrade' ) , ( 'Sec-WebSocket-Accept' , sec_ws_accept ) , ( 'Sec-WebSocket-Version' , str ( websocket_version ) ) ] #NEWLINE# if ( environ . get ( 'HTTP_SEC_WEBSOCKET_PROTOCOL' ) is not None ) : #NEWLINE# #INDENT# headers . append ( ( 'Sec-WebSocket-Protocol' , environ . get ( 'HTTP_SEC_WEBSOCKET_PROTOCOL' ) ) ) #NEWLINE# #UNINDENT# logger . debug ( 'WebSocket request accepted, switching protocols' ) #NEWLINE# start_response ( force_str ( '101 Switching Protocols' ) , headers ) #NEWLINE# six . get_method_self ( start_response ) . finish_content ( ) #NEWLINE# return WebSocket ( environ [ 'wsgi.input' ] )