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

Method accept

src/websockets/server.py:113–203  ·  view source on GitHub ↗

Create a handshake response to accept the connection. If the handshake request is valid and the handshake successful, :meth:`accept` returns an HTTP response with status code 101. Else, it returns an HTTP response with another status code. This rejects the

(self, request: Request)

Source from the content-addressed store, hash-verified

111 )
112
113 def accept(self, request: Request) -> Response:
114 """
115 Create a handshake response to accept the connection.
116
117 If the handshake request is valid and the handshake successful,
118 :meth:`accept` returns an HTTP response with status code 101.
119
120 Else, it returns an HTTP response with another status code. This rejects
121 the connection, like :meth:`reject` would.
122
123 You must send the handshake response with :meth:`send_response`.
124
125 You may modify the response before sending it, typically by adding HTTP
126 headers.
127
128 Args:
129 request: WebSocket handshake request received from the client.
130
131 Returns:
132 WebSocket handshake response or HTTP response to send to the client.
133
134 """
135 try:
136 (
137 accept_header,
138 extensions_header,
139 protocol_header,
140 ) = self.process_request(request)
141 except InvalidOrigin as exc:
142 request._exception = exc
143 self.handshake_exc = exc
144 if self.debug:
145 self.logger.debug("! invalid origin", exc_info=True)
146 return self.reject(
147 http.HTTPStatus.FORBIDDEN,
148 f"Failed to open a WebSocket connection: {exc}.\n",
149 )
150 except InvalidUpgrade as exc:
151 request._exception = exc
152 self.handshake_exc = exc
153 if self.debug:
154 self.logger.debug("! invalid upgrade", exc_info=True)
155 response = self.reject(
156 http.HTTPStatus.UPGRADE_REQUIRED,
157 (
158 f"Failed to open a WebSocket connection: {exc}.\n"
159 f"\n"
160 f"You cannot access a WebSocket server directly "
161 f"with a browser. You need a WebSocket client.\n"
162 ),
163 )
164 response.headers["Upgrade"] = "websocket"
165 return response
166 except InvalidHandshake as exc:
167 request._exception = exc
168 self.handshake_exc = exc
169 if self.debug:
170 self.logger.debug("! invalid handshake", exc_info=True)

Callers 15

test_accept_responseMethod · 0.95
test_unexpected_errorMethod · 0.95
test_basicMethod · 0.95
test_missing_upgradeMethod · 0.95
test_invalid_upgradeMethod · 0.95
test_missing_keyMethod · 0.95
test_multiple_keyMethod · 0.95
test_invalid_keyMethod · 0.95

Calls 4

process_requestMethod · 0.95
rejectMethod · 0.95
HeadersClass · 0.85
ResponseClass · 0.85

Tested by 15

test_accept_responseMethod · 0.76
test_unexpected_errorMethod · 0.76
test_basicMethod · 0.76
test_missing_upgradeMethod · 0.76
test_invalid_upgradeMethod · 0.76
test_missing_keyMethod · 0.76
test_multiple_keyMethod · 0.76
test_invalid_keyMethod · 0.76