Connect to the WebSocket server at ``uri``. This function returns a :class:`ClientConnection` instance, which you can use to send and receive messages. :func:`connect` may be used as a context manager:: from websockets.sync.client import connect with connect(...)
(
uri: str,
*,
# TCP/TLS
sock: socket.socket | None = None,
ssl: ssl_module.SSLContext | None = None,
server_hostname: str | None = None,
# 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,
proxy_ssl: ssl_module.SSLContext | None = None,
proxy_server_hostname: str | None = None,
# 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,
# Logging
logger: LoggerLike | None = None,
# Escape hatch for advanced customization
create_connection: type[ClientConnection] | None = None,
**kwargs: Any,
)
| 128 | |
| 129 | |
| 130 | def connect( |
| 131 | uri: str, |
| 132 | *, |
| 133 | # TCP/TLS |
| 134 | sock: socket.socket | None = None, |
| 135 | ssl: ssl_module.SSLContext | None = None, |
| 136 | server_hostname: str | None = None, |
| 137 | # WebSocket |
| 138 | origin: Origin | None = None, |
| 139 | extensions: Sequence[ClientExtensionFactory] | None = None, |
| 140 | subprotocols: Sequence[Subprotocol] | None = None, |
| 141 | compression: str | None = "deflate", |
| 142 | # HTTP |
| 143 | additional_headers: HeadersLike | None = None, |
| 144 | user_agent_header: str | None = USER_AGENT, |
| 145 | proxy: str | Literal[True] | None = True, |
| 146 | proxy_ssl: ssl_module.SSLContext | None = None, |
| 147 | proxy_server_hostname: str | None = None, |
| 148 | # Timeouts |
| 149 | open_timeout: float | None = 10, |
| 150 | ping_interval: float | None = 20, |
| 151 | ping_timeout: float | None = 20, |
| 152 | close_timeout: float | None = 10, |
| 153 | # Limits |
| 154 | max_size: int | None | tuple[int | None, int | None] = 2**20, |
| 155 | max_queue: int | None | tuple[int | None, int | None] = 16, |
| 156 | # Logging |
| 157 | logger: LoggerLike | None = None, |
| 158 | # Escape hatch for advanced customization |
| 159 | create_connection: type[ClientConnection] | None = None, |
| 160 | **kwargs: Any, |
| 161 | ) -> ClientConnection: |
| 162 | """ |
| 163 | Connect to the WebSocket server at ``uri``. |
| 164 | |
| 165 | This function returns a :class:`ClientConnection` instance, which you can |
| 166 | use to send and receive messages. |
| 167 | |
| 168 | :func:`connect` may be used as a context manager:: |
| 169 | |
| 170 | from websockets.sync.client import connect |
| 171 | |
| 172 | with connect(...) as websocket: |
| 173 | ... |
| 174 | |
| 175 | The connection is closed automatically when exiting the context. |
| 176 | |
| 177 | Args: |
| 178 | uri: URI of the WebSocket server. |
| 179 | sock: Preexisting TCP socket. ``sock`` overrides the host and port |
| 180 | from ``uri``. You may call :func:`socket.create_connection` to |
| 181 | create a suitable TCP socket. |
| 182 | ssl: Configuration for enabling TLS on the connection. |
| 183 | server_hostname: Host name for the TLS handshake. ``server_hostname`` |
| 184 | overrides the host name from ``uri``. |
| 185 | origin: Value of the ``Origin`` header, for servers that require it. |
| 186 | extensions: List of supported extensions, in order in which they |
| 187 | should be negotiated and run. |
searching dependent graphs…