def negotiate_websocket_version(uri_opener, websocket_url):
 for version in {13, 12, 14}:
  upgrade_request = build_ws_upgrade_request(websocket_url, web_socket_version=version)
  try:
   upgrade_response = uri_opener.send_mutant(upgrade_request)
  except HTTPRequestException:
   continue
  upgrade_code = upgrade_response.get_code()
  if (upgrade_code in {101}):
   return version
  elif (upgrade_code in {400}):
   headers = upgrade_response.get_headers()
   (version, _) = headers.iget('Sec-WebSocket-Version', None)
   if (version is None):
    continue
   return version
  else:
   continue
 return DEFAULT_PROTOCOL_VERSION

def negotiate_websocket_version ( uri_opener , websocket_url ) : for version in { 13 , 12 , 14 } : upgrade_request = build_ws_upgrade_request ( websocket_url , web_socket_version = version ) try : upgrade_response = uri_opener . send_mutant ( upgrade_request ) except HTTPRequestException : continue upgrade_code = upgrade_response . get_code ( ) if ( upgrade_code in { 101 } ) : return version elif ( upgrade_code in { 400 } ) : headers = upgrade_response . get_headers ( ) ( version , _ ) = headers . iget ( 'Sec-WebSocket-Version' , None ) if ( version is None ) : continue return version else : continue return DEFAULT_PROTOCOL_VERSION
try to find the websocket version used to talk to the server .