(
self,
uri: str,
*,
# WebSocket
origin: Origin | None = None,
extensions: Sequence[ClientExtensionFactory] | None = None,
subprotocols: Sequence[Subprotocol] | None = None,
compression: str | None = "deflate",
# HTTP
additional_headers: HeadersLike | None = None,
user_agent_header: str | None = USER_AGENT,
proxy: str | Literal[True] | None = True,
process_exception: Callable[[Exception], Exception | None] = process_exception,
# Timeouts
open_timeout: float | None = 10,
ping_interval: float | None = 20,
ping_timeout: float | None = 20,
close_timeout: float | None = 10,
# Limits
max_size: int | None | tuple[int | None, int | None] = 2**20,
max_queue: int | None | tuple[int | None, int | None] = 16,
write_limit: int | tuple[int, int | None] = 2**15,
# Logging
logger: LoggerLike | None = None,
# Escape hatch for advanced customization
create_connection: type[ClientConnection] | None = None,
# Other keyword arguments are passed to loop.create_connection
**kwargs: Any,
)
| 298 | """ |
| 299 | |
| 300 | def __init__( |
| 301 | self, |
| 302 | uri: str, |
| 303 | *, |
| 304 | # WebSocket |
| 305 | origin: Origin | None = None, |
| 306 | extensions: Sequence[ClientExtensionFactory] | None = None, |
| 307 | subprotocols: Sequence[Subprotocol] | None = None, |
| 308 | compression: str | None = "deflate", |
| 309 | # HTTP |
| 310 | additional_headers: HeadersLike | None = None, |
| 311 | user_agent_header: str | None = USER_AGENT, |
| 312 | proxy: str | Literal[True] | None = True, |
| 313 | process_exception: Callable[[Exception], Exception | None] = process_exception, |
| 314 | # Timeouts |
| 315 | open_timeout: float | None = 10, |
| 316 | ping_interval: float | None = 20, |
| 317 | ping_timeout: float | None = 20, |
| 318 | close_timeout: float | None = 10, |
| 319 | # Limits |
| 320 | max_size: int | None | tuple[int | None, int | None] = 2**20, |
| 321 | max_queue: int | None | tuple[int | None, int | None] = 16, |
| 322 | write_limit: int | tuple[int, int | None] = 2**15, |
| 323 | # Logging |
| 324 | logger: LoggerLike | None = None, |
| 325 | # Escape hatch for advanced customization |
| 326 | create_connection: type[ClientConnection] | None = None, |
| 327 | # Other keyword arguments are passed to loop.create_connection |
| 328 | **kwargs: Any, |
| 329 | ) -> None: |
| 330 | self.uri = uri |
| 331 | |
| 332 | if subprotocols is not None: |
| 333 | validate_subprotocols(subprotocols) |
| 334 | |
| 335 | if compression == "deflate": |
| 336 | extensions = enable_client_permessage_deflate(extensions) |
| 337 | elif compression is not None: |
| 338 | raise ValueError(f"unsupported compression: {compression}") |
| 339 | |
| 340 | if logger is None: |
| 341 | logger = logging.getLogger("websockets.client") |
| 342 | |
| 343 | if create_connection is None: |
| 344 | create_connection = ClientConnection |
| 345 | |
| 346 | def protocol_factory(uri: WebSocketURI) -> ClientConnection: |
| 347 | # This is a protocol in the Sans-I/O implementation of websockets. |
| 348 | protocol = ClientProtocol( |
| 349 | uri, |
| 350 | origin=origin, |
| 351 | extensions=extensions, |
| 352 | subprotocols=subprotocols, |
| 353 | max_size=max_size, |
| 354 | logger=logger, |
| 355 | ) |
| 356 | # This is a connection in websockets and a protocol in asyncio. |
| 357 | connection = create_connection( |
no test coverage detected