MCPcopy Create free account
hub / github.com/python-websockets/websockets / Connection

Class Connection

src/websockets/sync/connection.py:32–1078  ·  view source on GitHub ↗

:mod:`threading` implementation of a WebSocket connection. :class:`Connection` provides APIs shared between WebSocket servers and clients. You shouldn't use it directly. Instead, use :class:`~websockets.sync.client.ClientConnection` or :class:`~websockets.sync.server.Serve

Source from the content-addressed store, hash-verified

30
31
32class Connection:
33 """
34 :mod:`threading` implementation of a WebSocket connection.
35
36 :class:`Connection` provides APIs shared between WebSocket servers and
37 clients.
38
39 You shouldn't use it directly. Instead, use
40 :class:`~websockets.sync.client.ClientConnection` or
41 :class:`~websockets.sync.server.ServerConnection`.
42
43 """
44
45 recv_bufsize = 65536
46
47 def __init__(
48 self,
49 socket: socket.socket,
50 protocol: Protocol,
51 *,
52 ping_interval: float | None = 20,
53 ping_timeout: float | None = 20,
54 close_timeout: float | None = 10,
55 max_queue: int | None | tuple[int | None, int | None] = 16,
56 ) -> None:
57 self.socket = socket
58 self.protocol = protocol
59 self.ping_interval = ping_interval
60 self.ping_timeout = ping_timeout
61 self.close_timeout = close_timeout
62 if isinstance(max_queue, int) or max_queue is None:
63 max_queue_high, max_queue_low = max_queue, None
64 else:
65 max_queue_high, max_queue_low = max_queue
66
67 # Inject reference to this instance in the protocol's logger.
68 self.protocol.logger = logging.LoggerAdapter(
69 self.protocol.logger,
70 {"websocket": self},
71 )
72
73 # Copy attributes from the protocol for convenience.
74 self.id: uuid.UUID = self.protocol.id
75 """Unique identifier of the connection. Useful in logs."""
76 self.logger: LoggerLike = self.protocol.logger
77 """Logger for this connection."""
78 self.debug = self.protocol.debug
79
80 # HTTP handshake request and response.
81 self.request: Request | None = None
82 """Opening handshake request."""
83 self.response: Response | None = None
84 """Opening handshake response."""
85
86 # Mutex serializing interactions with the protocol.
87 self.protocol_mutex = threading.Lock()
88
89 # Lock stopping reads when the assembler buffer is full.

Callers 12

asyncSetUpMethod · 0.50
test_close_timeoutMethod · 0.50
test_max_queueMethod · 0.50
test_max_queue_noneMethod · 0.50
test_max_queue_tupleMethod · 0.50
test_write_limitMethod · 0.50
test_write_limitsMethod · 0.50
setUpMethod · 0.50
test_close_timeoutMethod · 0.50
test_max_queueMethod · 0.50
test_max_queue_noneMethod · 0.50
test_max_queue_tupleMethod · 0.50

Calls

no outgoing calls

Tested by 12

asyncSetUpMethod · 0.40
test_close_timeoutMethod · 0.40
test_max_queueMethod · 0.40
test_max_queue_noneMethod · 0.40
test_max_queue_tupleMethod · 0.40
test_write_limitMethod · 0.40
test_write_limitsMethod · 0.40
setUpMethod · 0.40
test_close_timeoutMethod · 0.40
test_max_queueMethod · 0.40
test_max_queue_noneMethod · 0.40
test_max_queue_tupleMethod · 0.40

Used in the wild real call sites across dependent graphs

searching dependent graphs…