MCPcopy Index your code
hub / github.com/websocket-client/websocket-client / close

Method close

websocket/_core.py:503–554  ·  view source on GitHub ↗

Close Websocket object Parameters ---------- status: int Status code to send. See VALID_CLOSE_STATUS in ABNF. reason: bytes The reason to close in UTF-8. timeout: int or float Timeout until receive a close frame.

(self, status: int = STATUS_NORMAL, reason: bytes = b"", timeout: int = 3)

Source from the content-addressed store, hash-verified

501 self.send(struct.pack("!H", status) + reason, ABNF.OPCODE_CLOSE)
502
503 def close(self, status: int = STATUS_NORMAL, reason: bytes = b"", timeout: int = 3):
504 """
505 Close Websocket object
506
507 Parameters
508 ----------
509 status: int
510 Status code to send. See VALID_CLOSE_STATUS in ABNF.
511 reason: bytes
512 The reason to close in UTF-8.
513 timeout: int or float
514 Timeout until receive a close frame.
515 If None, it will wait forever until receive a close frame.
516 """
517 if not self.connected:
518 return
519 if status < 0 or status >= ABNF.LENGTH_16:
520 raise ValueError("code is invalid range")
521
522 try:
523 self.connected = False
524 self.send(struct.pack("!H", status) + reason, ABNF.OPCODE_CLOSE)
525 if self.sock is None:
526 return
527 sock_timeout = self.sock.gettimeout()
528 self.sock.settimeout(timeout)
529 start_time = time.time()
530 while timeout is None or time.time() - start_time < timeout:
531 try:
532 frame = self.recv_frame()
533 if frame.opcode != ABNF.OPCODE_CLOSE:
534 continue
535 if isEnabledForError():
536 recv_status = struct.unpack("!H", frame.data[0:2])[0]
537 if recv_status >= 3000 and recv_status <= 4999:
538 debug(f"close status: {repr(recv_status)}")
539 elif recv_status != STATUS_NORMAL:
540 error(f"close status: {repr(recv_status)}")
541 break
542 except (
543 WebSocketConnectionClosedException,
544 WebSocketTimeoutException,
545 struct.error,
546 ):
547 break
548 if self.sock is not None:
549 self.sock.settimeout(sock_timeout)
550 self.sock.shutdown(socket.SHUT_RDWR)
551 except:
552 pass
553
554 self.shutdown()
555
556 def abort(self):
557 """

Callers 15

test_ssloptMethod · 0.95
test_closeMethod · 0.95
connectFunction · 0.45
_open_socketFunction · 0.45
connectMethod · 0.45
shutdownMethod · 0.45
_recvMethod · 0.45
_recvFunction · 0.45
_sendFunction · 0.45
readMethod · 0.45
readMethod · 0.45

Calls 8

sendMethod · 0.95
recv_frameMethod · 0.95
shutdownMethod · 0.95
isEnabledForErrorFunction · 0.85
debugFunction · 0.85
errorFunction · 0.85
settimeoutMethod · 0.80
gettimeoutMethod · 0.45

Tested by 3

test_ssloptMethod · 0.76
test_closeMethod · 0.76