Create TCP or Unix connection.
(self)
| 374 | self.connection_kwargs = kwargs |
| 375 | |
| 376 | async def create_connection(self) -> ClientConnection: |
| 377 | """Create TCP or Unix connection.""" |
| 378 | loop = asyncio.get_running_loop() |
| 379 | kwargs = self.connection_kwargs.copy() |
| 380 | |
| 381 | ws_uri = parse_uri(self.uri) |
| 382 | |
| 383 | proxy = self.proxy |
| 384 | if kwargs.get("unix", False): |
| 385 | proxy = None |
| 386 | if kwargs.get("sock") is not None: |
| 387 | proxy = None |
| 388 | if proxy is True: |
| 389 | proxy = get_proxy(ws_uri) |
| 390 | |
| 391 | def factory() -> ClientConnection: |
| 392 | return self.protocol_factory(ws_uri) |
| 393 | |
| 394 | if ws_uri.secure: |
| 395 | kwargs.setdefault("ssl", True) |
| 396 | kwargs.setdefault("server_hostname", ws_uri.host) |
| 397 | if kwargs.get("ssl") is None: |
| 398 | raise ValueError("ssl=None is incompatible with a wss:// URI") |
| 399 | else: |
| 400 | if kwargs.get("ssl") is not None: |
| 401 | raise ValueError("ssl argument is incompatible with a ws:// URI") |
| 402 | |
| 403 | if kwargs.pop("unix", False): |
| 404 | _, connection = await loop.create_unix_connection(factory, **kwargs) |
| 405 | elif proxy is not None: |
| 406 | proxy_parsed = parse_proxy(proxy) |
| 407 | if proxy_parsed.scheme[:5] == "socks": |
| 408 | # Connect to the server through the proxy. |
| 409 | sock = await connect_socks_proxy( |
| 410 | proxy_parsed, |
| 411 | ws_uri, |
| 412 | local_addr=kwargs.pop("local_addr", None), |
| 413 | ) |
| 414 | # Initialize WebSocket connection via the proxy. |
| 415 | _, connection = await loop.create_connection( |
| 416 | factory, |
| 417 | sock=sock, |
| 418 | **kwargs, |
| 419 | ) |
| 420 | elif proxy_parsed.scheme[:4] == "http": |
| 421 | # Split keyword arguments between the proxy and the server. |
| 422 | all_kwargs, proxy_kwargs, kwargs = kwargs, {}, {} |
| 423 | for key, value in all_kwargs.items(): |
| 424 | if key.startswith("ssl") or key == "server_hostname": |
| 425 | kwargs[key] = value |
| 426 | elif key.startswith("proxy_"): |
| 427 | proxy_kwargs[key[6:]] = value |
| 428 | else: |
| 429 | proxy_kwargs[key] = value |
| 430 | # Validate the proxy_ssl argument. |
| 431 | if proxy_parsed.scheme == "https": |
| 432 | proxy_kwargs.setdefault("ssl", True) |
| 433 | if proxy_kwargs.get("ssl") is None: |
no test coverage detected