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

Function conn_handler

src/websockets/sync/server.py:523–620  ·  view source on GitHub ↗
(sock: socket.socket, addr: Any)

Source from the content-addressed store, hash-verified

521 # Define request handler
522
523 def conn_handler(sock: socket.socket, addr: Any) -> None:
524 # Calculate timeouts on the TLS and WebSocket handshakes.
525 # The TLS timeout must be set on the socket, then removed
526 # to avoid conflicting with the WebSocket timeout in handshake().
527 deadline = Deadline(open_timeout)
528
529 try:
530 # Disable Nagle algorithm
531
532 if not unix:
533 sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
534
535 # Perform TLS handshake
536
537 if ssl is not None:
538 sock.settimeout(deadline.timeout())
539 # mypy cannot figure this out
540 assert isinstance(sock, ssl_module.SSLSocket)
541 sock.do_handshake()
542 sock.settimeout(None)
543
544 # Create a closure to give select_subprotocol access to connection.
545 protocol_select_subprotocol: (
546 Callable[
547 [ServerProtocol, Sequence[Subprotocol]],
548 Subprotocol | None,
549 ]
550 | None
551 ) = None
552 if select_subprotocol is not None:
553
554 def protocol_select_subprotocol(
555 protocol: ServerProtocol,
556 subprotocols: Sequence[Subprotocol],
557 ) -> Subprotocol | None:
558 # mypy doesn't know that select_subprotocol is immutable.
559 assert select_subprotocol is not None
560 # Ensure this function is only used in the intended context.
561 assert protocol is connection.protocol
562 return select_subprotocol(connection, subprotocols)
563
564 # Initialize WebSocket protocol
565
566 protocol = ServerProtocol(
567 origins=origins,
568 extensions=extensions,
569 subprotocols=subprotocols,
570 select_subprotocol=protocol_select_subprotocol,
571 max_size=max_size,
572 logger=logger,
573 )
574
575 # Initialize WebSocket connection
576
577 assert create_connection is not None # help mypy
578 connection = create_connection(
579 sock,
580 protocol,

Callers

nothing calls this directly

Calls 8

timeoutMethod · 0.95
DeadlineClass · 0.85
ServerProtocolClass · 0.85
close_socketMethod · 0.80
handlerFunction · 0.50
closeMethod · 0.45
handshakeMethod · 0.45
start_keepaliveMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…