MCPcopy
hub / github.com/aio-libs/aiohttp / _ws_connect

Method _ws_connect

aiohttp/client.py:265–347  ·  view source on GitHub ↗
(self, url, *,
                    protocols=(),
                    timeout=10.0,
                    autoclose=True,
                    autoping=True,
                    auth=None,
                    origin=None)

Source from the content-addressed store, hash-verified

263
264 @asyncio.coroutine
265 def _ws_connect(self, url, *,
266 protocols=(),
267 timeout=10.0,
268 autoclose=True,
269 autoping=True,
270 auth=None,
271 origin=None):
272
273 sec_key = base64.b64encode(os.urandom(16))
274
275 headers = {
276 hdrs.UPGRADE: hdrs.WEBSOCKET,
277 hdrs.CONNECTION: hdrs.UPGRADE,
278 hdrs.SEC_WEBSOCKET_VERSION: '13',
279 hdrs.SEC_WEBSOCKET_KEY: sec_key.decode(),
280 }
281 if protocols:
282 headers[hdrs.SEC_WEBSOCKET_PROTOCOL] = ','.join(protocols)
283 if origin is not None:
284 headers[hdrs.ORIGIN] = origin
285
286 # send request
287 resp = yield from self.get(url, headers=headers,
288 read_until_eof=False,
289 auth=auth)
290
291 try:
292 # check handshake
293 if resp.status != 101:
294 raise WSServerHandshakeError(
295 message='Invalid response status',
296 code=resp.status,
297 headers=resp.headers)
298
299 if resp.headers.get(hdrs.UPGRADE, '').lower() != 'websocket':
300 raise WSServerHandshakeError(
301 message='Invalid upgrade header',
302 code=resp.status,
303 headers=resp.headers)
304
305 if resp.headers.get(hdrs.CONNECTION, '').lower() != 'upgrade':
306 raise WSServerHandshakeError(
307 message='Invalid connection header',
308 code=resp.status,
309 headers=resp.headers)
310
311 # key calculation
312 key = resp.headers.get(hdrs.SEC_WEBSOCKET_ACCEPT, '')
313 match = base64.b64encode(
314 hashlib.sha1(sec_key + WS_KEY).digest()).decode()
315 if key != match:
316 raise WSServerHandshakeError(
317 message='Invalid challenge response',
318 code=resp.status,
319 headers=resp.headers)
320
321 # websocket protocol
322 protocol = None

Callers 2

ws_connectMethod · 0.95
ws_connectFunction · 0.95

Calls 7

getMethod · 0.95
WebSocketWriterClass · 0.85
decodeMethod · 0.80
set_parserMethod · 0.80
set_tcp_nodelayMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected