MCPcopy Index your code
hub / github.com/python-websockets/websockets / ClientProtocol

Class ClientProtocol

src/websockets/client.py:48–341  ·  view source on GitHub ↗

Sans-I/O implementation of a WebSocket client connection. Args: uri: URI of the WebSocket server, parsed with :func:`~websockets.uri.parse_uri`. origin: Value of the ``Origin`` header. This is useful when connecting to a server that validates the ``O

Source from the content-addressed store, hash-verified

46
47
48class ClientProtocol(Protocol):
49 """
50 Sans-I/O implementation of a WebSocket client connection.
51
52 Args:
53 uri: URI of the WebSocket server, parsed
54 with :func:`~websockets.uri.parse_uri`.
55 origin: Value of the ``Origin`` header. This is useful when connecting
56 to a server that validates the ``Origin`` header to defend against
57 Cross-Site WebSocket Hijacking attacks.
58 extensions: List of supported extensions, in order in which they
59 should be tried.
60 subprotocols: List of supported subprotocols, in order of decreasing
61 preference.
62 state: Initial state of the WebSocket connection.
63 max_size: Maximum size of incoming messages in bytes.
64 :obj:`None` disables the limit. You may pass a ``(max_message_size,
65 max_fragment_size)`` tuple to set different limits for messages and
66 fragments when you expect long messages sent in short fragments.
67 logger: Logger for this connection;
68 defaults to ``logging.getLogger("websockets.client")``;
69 see the :doc:`logging guide <../../topics/logging>` for details.
70
71 """
72
73 def __init__(
74 self,
75 uri: WebSocketURI,
76 *,
77 origin: Origin | None = None,
78 extensions: Sequence[ClientExtensionFactory] | None = None,
79 subprotocols: Sequence[Subprotocol] | None = None,
80 state: State = CONNECTING,
81 max_size: int | None | tuple[int | None, int | None] = 2**20,
82 logger: LoggerLike | None = None,
83 ) -> None:
84 super().__init__(
85 side=CLIENT,
86 state=state,
87 max_size=max_size,
88 logger=logger,
89 )
90 self.uri = uri
91 self.origin = origin
92 self.available_extensions = extensions
93 self.available_subprotocols = subprotocols
94 self.key = generate_key()
95
96 def connect(self) -> Request:
97 """
98 Create a handshake request to open a connection.
99
100 You must send the handshake request with :meth:`send_request`.
101
102 You can modify it before sending it, for example to add HTTP headers.
103
104 Returns:
105 WebSocket handshake request event to send to the server.

Callers 15

protocol_factoryMethod · 0.85
connectFunction · 0.85
test_send_requestMethod · 0.85
test_connectMethod · 0.85
test_pathMethod · 0.85
test_portMethod · 0.85
test_user_infoMethod · 0.85
test_originMethod · 0.85
test_extensionsMethod · 0.85
test_subprotocolsMethod · 0.85

Calls

no outgoing calls

Tested by 15

test_send_requestMethod · 0.68
test_connectMethod · 0.68
test_pathMethod · 0.68
test_portMethod · 0.68
test_user_infoMethod · 0.68
test_originMethod · 0.68
test_extensionsMethod · 0.68
test_subprotocolsMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…