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

Method connect

src/websockets/client.py:96–129  ·  view source on GitHub ↗

Create a handshake request to open a connection. You must send the handshake request with :meth:`send_request`. You can modify it before sending it, for example to add HTTP headers. Returns: WebSocket handshake request event to send to the server.

(self)

Source from the content-addressed store, hash-verified

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.
106
107 """
108 headers = Headers()
109 headers["Host"] = build_host(self.uri.host, self.uri.port, self.uri.secure)
110 if self.uri.user_info:
111 headers["Authorization"] = build_authorization_basic(*self.uri.user_info)
112 if self.origin is not None:
113 headers["Origin"] = self.origin
114 headers["Upgrade"] = "websocket"
115 headers["Connection"] = "Upgrade"
116 headers["Sec-WebSocket-Key"] = self.key
117 headers["Sec-WebSocket-Version"] = "13"
118 if self.available_extensions is not None:
119 headers["Sec-WebSocket-Extensions"] = build_extension(
120 [
121 (extension_factory.name, extension_factory.get_request_params())
122 for extension_factory in self.available_extensions
123 ]
124 )
125 if self.available_subprotocols is not None:
126 headers["Sec-WebSocket-Protocol"] = build_subprotocol(
127 self.available_subprotocols
128 )
129 return Request(self.uri.resource_name, headers)
130
131 def process_response(self, response: Response) -> None:
132 """

Callers 15

test_send_requestMethod · 0.95
test_connectMethod · 0.95
test_pathMethod · 0.95
test_portMethod · 0.95
test_user_infoMethod · 0.95
test_originMethod · 0.95
test_extensionsMethod · 0.95
test_subprotocolsMethod · 0.95
test_offer_extensionMethod · 0.95
handshakeMethod · 0.80
connect_socks_proxyFunction · 0.80

Calls 7

HeadersClass · 0.85
build_hostFunction · 0.85
build_extensionFunction · 0.85
build_subprotocolFunction · 0.85
RequestClass · 0.85
get_request_paramsMethod · 0.45

Tested by 11

test_send_requestMethod · 0.76
test_connectMethod · 0.76
test_pathMethod · 0.76
test_portMethod · 0.76
test_user_infoMethod · 0.76
test_originMethod · 0.76
test_extensionsMethod · 0.76
test_subprotocolsMethod · 0.76
test_offer_extensionMethod · 0.76
test_explicit_socketMethod · 0.64