Code and reason for WebSocket close frames. Attributes: code: Close code. reason: Close reason.
| 351 | |
| 352 | @dataclasses.dataclass |
| 353 | class Close: |
| 354 | """ |
| 355 | Code and reason for WebSocket close frames. |
| 356 | |
| 357 | Attributes: |
| 358 | code: Close code. |
| 359 | reason: Close reason. |
| 360 | |
| 361 | """ |
| 362 | |
| 363 | code: CloseCode | int |
| 364 | reason: str |
| 365 | |
| 366 | def __str__(self) -> str: |
| 367 | """ |
| 368 | Return a human-readable representation of a close code and reason. |
| 369 | |
| 370 | """ |
| 371 | if 3000 <= self.code < 4000: |
| 372 | explanation = "registered" |
| 373 | elif 4000 <= self.code < 5000: |
| 374 | explanation = "private use" |
| 375 | else: |
| 376 | explanation = CLOSE_CODE_EXPLANATIONS.get(self.code, "unknown") |
| 377 | result = f"{self.code} ({explanation})" |
| 378 | |
| 379 | if self.reason: |
| 380 | result = f"{result} {self.reason}" |
| 381 | |
| 382 | return result |
| 383 | |
| 384 | @classmethod |
| 385 | def parse(cls, data: BytesLike) -> Close: |
| 386 | """ |
| 387 | Parse the payload of a close frame. |
| 388 | |
| 389 | Args: |
| 390 | data: Payload of the close frame. |
| 391 | |
| 392 | Raises: |
| 393 | ProtocolError: If data is ill-formed. |
| 394 | UnicodeDecodeError: If the reason isn't valid UTF-8. |
| 395 | |
| 396 | """ |
| 397 | if isinstance(data, memoryview): |
| 398 | raise AssertionError("only compressed outgoing frames use memoryview") |
| 399 | if len(data) >= 2: |
| 400 | (code,) = struct.unpack("!H", data[:2]) |
| 401 | reason = data[2:].decode() |
| 402 | close = cls(code, reason) |
| 403 | close.check() |
| 404 | return close |
| 405 | elif len(data) == 0: |
| 406 | return cls(CloseCode.NO_STATUS_RCVD, "") |
| 407 | else: |
| 408 | raise ProtocolError("close frame too short") |
| 409 | |
| 410 | def serialize(self) -> bytes: |
no outgoing calls
searching dependent graphs…