client.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. from __future__ import annotations
  2. import logging
  3. import socket
  4. import ssl as ssl_module
  5. import threading
  6. import warnings
  7. from collections.abc import Sequence
  8. from typing import Any, Callable, Literal, TypeVar, cast
  9. from ..client import ClientProtocol
  10. from ..datastructures import HeadersLike
  11. from ..exceptions import InvalidProxyMessage, InvalidProxyStatus, ProxyError
  12. from ..extensions.base import ClientExtensionFactory
  13. from ..extensions.permessage_deflate import enable_client_permessage_deflate
  14. from ..headers import validate_subprotocols
  15. from ..http11 import USER_AGENT, Response
  16. from ..protocol import CONNECTING, Event
  17. from ..proxy import Proxy, get_proxy, parse_proxy, prepare_connect_request
  18. from ..streams import StreamReader
  19. from ..typing import BytesLike, LoggerLike, Origin, Subprotocol
  20. from ..uri import WebSocketURI, parse_uri
  21. from .connection import Connection
  22. from .utils import Deadline
  23. __all__ = ["connect", "unix_connect", "ClientConnection"]
  24. class ClientConnection(Connection):
  25. """
  26. :mod:`threading` implementation of a WebSocket client connection.
  27. :class:`ClientConnection` provides :meth:`recv` and :meth:`send` methods for
  28. receiving and sending messages.
  29. It supports iteration to receive messages::
  30. for message in websocket:
  31. process(message)
  32. The iterator exits normally when the connection is closed with code
  33. 1000 (OK) or 1001 (going away) or without a close code. It raises a
  34. :exc:`~websockets.exceptions.ConnectionClosedError` when the connection is
  35. closed with any other code.
  36. The ``ping_interval``, ``ping_timeout``, ``close_timeout``, and
  37. ``max_queue`` arguments have the same meaning as in :func:`connect`.
  38. Args:
  39. socket: Socket connected to a WebSocket server.
  40. protocol: Sans-I/O connection.
  41. """
  42. def __init__(
  43. self,
  44. sock: socket.socket,
  45. protocol: ClientProtocol,
  46. *,
  47. ping_interval: float | None = 20,
  48. ping_timeout: float | None = 20,
  49. close_timeout: float | None = 10,
  50. max_queue: int | None | tuple[int | None, int | None] = 16,
  51. ) -> None:
  52. self.protocol: ClientProtocol
  53. self.response_rcvd = threading.Event()
  54. super().__init__(
  55. sock,
  56. protocol,
  57. ping_interval=ping_interval,
  58. ping_timeout=ping_timeout,
  59. close_timeout=close_timeout,
  60. max_queue=max_queue,
  61. )
  62. def handshake(
  63. self,
  64. additional_headers: HeadersLike | None = None,
  65. user_agent_header: str | None = USER_AGENT,
  66. timeout: float | None = None,
  67. ) -> None:
  68. """
  69. Perform the opening handshake.
  70. """
  71. self.request = self.protocol.connect()
  72. if additional_headers is not None:
  73. self.request.headers.update(additional_headers)
  74. if user_agent_header is not None:
  75. self.request.headers.setdefault("User-Agent", user_agent_header)
  76. with self.send_context(expected_state=CONNECTING):
  77. self.protocol.send_request(self.request)
  78. if not self.response_rcvd.wait(timeout):
  79. raise TimeoutError("timed out while waiting for handshake response")
  80. # self.protocol.handshake_exc is set when the connection is lost before
  81. # receiving a response, when the response cannot be parsed, or when the
  82. # response fails the handshake.
  83. if self.protocol.handshake_exc is not None:
  84. raise self.protocol.handshake_exc
  85. def process_event(self, event: Event) -> None:
  86. """
  87. Process one incoming event.
  88. """
  89. # First event - handshake response.
  90. if self.response is None:
  91. assert isinstance(event, Response)
  92. self.response = event
  93. self.response_rcvd.set()
  94. # Later events - frames.
  95. else:
  96. super().process_event(event)
  97. def recv_events(self) -> None:
  98. """
  99. Read incoming data from the socket and process events.
  100. """
  101. try:
  102. super().recv_events()
  103. finally:
  104. # If the connection is closed during the handshake, unblock it.
  105. self.response_rcvd.set()
  106. def connect(
  107. uri: str,
  108. *,
  109. # TCP/TLS
  110. sock: socket.socket | None = None,
  111. ssl: ssl_module.SSLContext | None = None,
  112. server_hostname: str | None = None,
  113. # WebSocket
  114. origin: Origin | None = None,
  115. extensions: Sequence[ClientExtensionFactory] | None = None,
  116. subprotocols: Sequence[Subprotocol] | None = None,
  117. compression: str | None = "deflate",
  118. # HTTP
  119. additional_headers: HeadersLike | None = None,
  120. user_agent_header: str | None = USER_AGENT,
  121. proxy: str | Literal[True] | None = True,
  122. proxy_ssl: ssl_module.SSLContext | None = None,
  123. proxy_server_hostname: str | None = None,
  124. # Timeouts
  125. open_timeout: float | None = 10,
  126. ping_interval: float | None = 20,
  127. ping_timeout: float | None = 20,
  128. close_timeout: float | None = 10,
  129. # Limits
  130. max_size: int | None | tuple[int | None, int | None] = 2**20,
  131. max_queue: int | None | tuple[int | None, int | None] = 16,
  132. # Logging
  133. logger: LoggerLike | None = None,
  134. # Escape hatch for advanced customization
  135. create_connection: type[ClientConnection] | None = None,
  136. # Other keyword arguments are passed to socket.create_connection
  137. **kwargs: Any,
  138. ) -> ClientConnection:
  139. """
  140. Connect to the WebSocket server at ``uri``.
  141. This function returns a :class:`ClientConnection` instance, which you can
  142. use to send and receive messages.
  143. :func:`connect` may be used as a context manager::
  144. from websockets.sync.client import connect
  145. with connect(...) as websocket:
  146. ...
  147. The connection is closed automatically when exiting the context.
  148. Args:
  149. uri: URI of the WebSocket server.
  150. sock: Preexisting TCP socket. ``sock`` overrides the host and port
  151. from ``uri``. You may call :func:`socket.create_connection` to
  152. create a suitable TCP socket.
  153. ssl: Configuration for enabling TLS on the connection.
  154. server_hostname: Host name for the TLS handshake. ``server_hostname``
  155. overrides the host name from ``uri``.
  156. origin: Value of the ``Origin`` header, for servers that require it.
  157. extensions: List of supported extensions, in order in which they
  158. should be negotiated and run.
  159. subprotocols: List of supported subprotocols, in order of decreasing
  160. preference.
  161. compression: The "permessage-deflate" extension is enabled by default.
  162. Set ``compression`` to :obj:`None` to disable it. See the
  163. :doc:`compression guide <../../topics/compression>` for details.
  164. additional_headers: Arbitrary HTTP headers to add to the handshake
  165. request.
  166. user_agent_header: Value of the ``User-Agent`` request header.
  167. It defaults to ``"Python/x.y.z websockets/X.Y"``.
  168. Setting it to :obj:`None` removes the header.
  169. proxy: If a proxy is configured, it is used by default. Set ``proxy``
  170. to :obj:`None` to disable the proxy or to the address of a proxy
  171. to override the system configuration. See the :doc:`proxy docs
  172. <../../topics/proxies>` for details.
  173. proxy_ssl: Configuration for enabling TLS on the proxy connection.
  174. proxy_server_hostname: Host name for the TLS handshake with the proxy.
  175. ``proxy_server_hostname`` overrides the host name from ``proxy``.
  176. open_timeout: Timeout for opening the connection in seconds.
  177. :obj:`None` disables the timeout.
  178. ping_interval: Interval between keepalive pings in seconds.
  179. :obj:`None` disables keepalive.
  180. ping_timeout: Timeout for keepalive pings in seconds.
  181. :obj:`None` disables timeouts.
  182. close_timeout: Timeout for closing the connection in seconds.
  183. :obj:`None` disables the timeout.
  184. max_size: Maximum size of incoming messages in bytes.
  185. :obj:`None` disables the limit. You may pass a ``(max_message_size,
  186. max_fragment_size)`` tuple to set different limits for messages and
  187. fragments when you expect long messages sent in short fragments.
  188. max_queue: High-water mark of the buffer where frames are received.
  189. It defaults to 16 frames. The low-water mark defaults to ``max_queue
  190. // 4``. You may pass a ``(high, low)`` tuple to set the high-water
  191. and low-water marks. If you want to disable flow control entirely,
  192. you may set it to ``None``, although that's a bad idea.
  193. logger: Logger for this client.
  194. It defaults to ``logging.getLogger("websockets.client")``.
  195. See the :doc:`logging guide <../../topics/logging>` for details.
  196. create_connection: Factory for the :class:`ClientConnection` managing
  197. the connection. Set it to a wrapper or a subclass to customize
  198. connection handling.
  199. Any other keyword arguments are passed to :func:`~socket.create_connection`.
  200. Raises:
  201. InvalidURI: If ``uri`` isn't a valid WebSocket URI.
  202. InvalidProxy: If ``proxy`` isn't a valid proxy.
  203. OSError: If the TCP connection fails.
  204. InvalidHandshake: If the opening handshake fails.
  205. TimeoutError: If the opening handshake times out.
  206. """
  207. # Process parameters
  208. # Backwards compatibility: ssl used to be called ssl_context.
  209. if ssl is None and "ssl_context" in kwargs:
  210. ssl = kwargs.pop("ssl_context")
  211. warnings.warn( # deprecated in 13.0 - 2024-08-20
  212. "ssl_context was renamed to ssl",
  213. DeprecationWarning,
  214. )
  215. ws_uri = parse_uri(uri)
  216. if not ws_uri.secure and ssl is not None:
  217. raise ValueError("ssl argument is incompatible with a ws:// URI")
  218. if subprotocols is not None:
  219. validate_subprotocols(subprotocols)
  220. if compression == "deflate":
  221. extensions = enable_client_permessage_deflate(extensions)
  222. elif compression is not None:
  223. raise ValueError(f"unsupported compression: {compression}")
  224. if logger is None:
  225. logger = logging.getLogger("websockets.client")
  226. if create_connection is None:
  227. create_connection = ClientConnection
  228. # Private APIs for unix_connect()
  229. unix: bool = kwargs.pop("unix", False)
  230. path: str | None = kwargs.pop("path", None)
  231. if unix:
  232. if path is None and sock is None:
  233. raise ValueError("missing path argument")
  234. elif path is not None and sock is not None:
  235. raise ValueError("path is incompatible with sock")
  236. if unix:
  237. proxy = None
  238. if sock is not None:
  239. proxy = None
  240. if proxy is True:
  241. proxy = get_proxy(ws_uri)
  242. # Calculate timeouts on the TCP, TLS, and WebSocket handshakes.
  243. # The TCP and TLS timeouts must be set on the socket, then removed
  244. # to avoid conflicting with the WebSocket timeout in handshake().
  245. deadline = Deadline(open_timeout)
  246. try:
  247. # Connect socket
  248. if sock is None:
  249. if unix:
  250. sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  251. sock.settimeout(deadline.timeout())
  252. assert path is not None # mypy cannot figure this out
  253. sock.connect(path)
  254. elif proxy is not None:
  255. proxy_parsed = parse_proxy(proxy)
  256. if proxy_parsed.scheme[:5] == "socks":
  257. sock = connect_socks_proxy(
  258. proxy_parsed,
  259. ws_uri,
  260. deadline,
  261. # websockets is consistent with the socket module while
  262. # python_socks is consistent across implementations.
  263. local_addr=kwargs.pop("source_address", None),
  264. )
  265. elif proxy_parsed.scheme[:4] == "http":
  266. if proxy_parsed.scheme != "https" and proxy_ssl is not None:
  267. raise ValueError(
  268. "proxy_ssl argument is incompatible with an http:// proxy"
  269. )
  270. sock = connect_http_proxy(
  271. proxy_parsed,
  272. ws_uri,
  273. deadline,
  274. user_agent_header=user_agent_header,
  275. ssl=proxy_ssl,
  276. server_hostname=proxy_server_hostname,
  277. **kwargs,
  278. )
  279. else:
  280. raise AssertionError("parse_proxy returned unsupported proxy")
  281. else: # proxy is None
  282. kwargs.setdefault("timeout", deadline.timeout())
  283. sock = socket.create_connection(
  284. (ws_uri.host, ws_uri.port),
  285. **kwargs,
  286. )
  287. sock.settimeout(None)
  288. # Disable Nagle algorithm
  289. if not unix:
  290. sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
  291. # Initialize TLS wrapper and perform TLS handshake
  292. if ws_uri.secure:
  293. if ssl is None:
  294. ssl = ssl_module.create_default_context()
  295. if server_hostname is None:
  296. server_hostname = ws_uri.host
  297. sock.settimeout(deadline.timeout())
  298. if proxy_ssl is None:
  299. sock = ssl.wrap_socket(sock, server_hostname=server_hostname)
  300. else:
  301. sock_2 = SSLSSLSocket(sock, ssl, server_hostname=server_hostname)
  302. # Let's pretend that sock is a socket, even though it isn't.
  303. sock = cast(socket.socket, sock_2)
  304. sock.settimeout(None)
  305. # Initialize WebSocket protocol
  306. protocol = ClientProtocol(
  307. ws_uri,
  308. origin=origin,
  309. extensions=extensions,
  310. subprotocols=subprotocols,
  311. max_size=max_size,
  312. logger=logger,
  313. )
  314. # Initialize WebSocket connection
  315. # create_connection defaults to ClientConnection.
  316. connection = create_connection(
  317. sock,
  318. protocol,
  319. ping_interval=ping_interval,
  320. ping_timeout=ping_timeout,
  321. close_timeout=close_timeout,
  322. max_queue=max_queue,
  323. )
  324. except Exception:
  325. if sock is not None:
  326. sock.close()
  327. raise
  328. try:
  329. connection.handshake(
  330. additional_headers,
  331. user_agent_header,
  332. deadline.timeout(),
  333. )
  334. except Exception:
  335. connection.close_socket()
  336. connection.recv_events_thread.join()
  337. raise
  338. connection.start_keepalive()
  339. return connection
  340. def unix_connect(
  341. path: str | None = None,
  342. uri: str | None = None,
  343. **kwargs: Any,
  344. ) -> ClientConnection:
  345. """
  346. Connect to a WebSocket server listening on a Unix socket.
  347. This function accepts the same keyword arguments as :func:`connect`.
  348. It's only available on Unix.
  349. It's mainly useful for debugging servers listening on Unix sockets.
  350. Args:
  351. path: File system path to the Unix socket.
  352. uri: URI of the WebSocket server. ``uri`` defaults to
  353. ``ws://localhost/`` or, when a ``ssl`` is provided, to
  354. ``wss://localhost/``.
  355. """
  356. if uri is None:
  357. # Backwards compatibility: ssl used to be called ssl_context.
  358. if kwargs.get("ssl") is None and kwargs.get("ssl_context") is None:
  359. uri = "ws://localhost/"
  360. else:
  361. uri = "wss://localhost/"
  362. return connect(uri=uri, unix=True, path=path, **kwargs)
  363. try:
  364. from python_socks import ProxyType
  365. from python_socks.sync import Proxy as SocksProxy
  366. except ImportError:
  367. def connect_socks_proxy(
  368. proxy: Proxy,
  369. ws_uri: WebSocketURI,
  370. deadline: Deadline,
  371. **kwargs: Any,
  372. ) -> socket.socket:
  373. raise ImportError("connecting through a SOCKS proxy requires python-socks")
  374. else:
  375. SOCKS_PROXY_TYPES = {
  376. "socks5h": ProxyType.SOCKS5,
  377. "socks5": ProxyType.SOCKS5,
  378. "socks4a": ProxyType.SOCKS4,
  379. "socks4": ProxyType.SOCKS4,
  380. }
  381. SOCKS_PROXY_RDNS = {
  382. "socks5h": True,
  383. "socks5": False,
  384. "socks4a": True,
  385. "socks4": False,
  386. }
  387. def connect_socks_proxy(
  388. proxy: Proxy,
  389. ws_uri: WebSocketURI,
  390. deadline: Deadline,
  391. **kwargs: Any,
  392. ) -> socket.socket:
  393. """Connect via a SOCKS proxy and return the socket."""
  394. socks_proxy = SocksProxy(
  395. SOCKS_PROXY_TYPES[proxy.scheme],
  396. proxy.host,
  397. proxy.port,
  398. proxy.username,
  399. proxy.password,
  400. SOCKS_PROXY_RDNS[proxy.scheme],
  401. )
  402. kwargs.setdefault("timeout", deadline.timeout())
  403. # connect() is documented to raise OSError and TimeoutError.
  404. # Wrap other exceptions in ProxyError, a subclass of InvalidHandshake.
  405. try:
  406. return socks_proxy.connect(ws_uri.host, ws_uri.port, **kwargs)
  407. except (OSError, TimeoutError, socket.timeout):
  408. raise
  409. except Exception as exc:
  410. raise ProxyError("failed to connect to SOCKS proxy") from exc
  411. def read_connect_response(sock: socket.socket, deadline: Deadline) -> Response:
  412. reader = StreamReader()
  413. parser = Response.parse(
  414. reader.read_line,
  415. reader.read_exact,
  416. reader.read_to_eof,
  417. proxy=True,
  418. )
  419. try:
  420. while True:
  421. sock.settimeout(deadline.timeout())
  422. data = sock.recv(4096)
  423. if data:
  424. reader.feed_data(data)
  425. else:
  426. reader.feed_eof()
  427. next(parser)
  428. except StopIteration as exc:
  429. assert isinstance(exc.value, Response) # help mypy
  430. response = exc.value
  431. if 200 <= response.status_code < 300:
  432. return response
  433. else:
  434. raise InvalidProxyStatus(response)
  435. except socket.timeout:
  436. raise TimeoutError("timed out while connecting to HTTP proxy")
  437. except Exception as exc:
  438. raise InvalidProxyMessage(
  439. "did not receive a valid HTTP response from proxy"
  440. ) from exc
  441. finally:
  442. sock.settimeout(None)
  443. def connect_http_proxy(
  444. proxy: Proxy,
  445. ws_uri: WebSocketURI,
  446. deadline: Deadline,
  447. *,
  448. user_agent_header: str | None = None,
  449. ssl: ssl_module.SSLContext | None = None,
  450. server_hostname: str | None = None,
  451. **kwargs: Any,
  452. ) -> socket.socket:
  453. # Connect socket
  454. kwargs.setdefault("timeout", deadline.timeout())
  455. sock = socket.create_connection((proxy.host, proxy.port), **kwargs)
  456. # Initialize TLS wrapper and perform TLS handshake
  457. if proxy.scheme == "https":
  458. if ssl is None:
  459. ssl = ssl_module.create_default_context()
  460. if server_hostname is None:
  461. server_hostname = proxy.host
  462. sock.settimeout(deadline.timeout())
  463. sock = ssl.wrap_socket(sock, server_hostname=server_hostname)
  464. sock.settimeout(None)
  465. # Send CONNECT request to the proxy and read response.
  466. request = prepare_connect_request(proxy, ws_uri, user_agent_header)
  467. sock.sendall(request)
  468. try:
  469. read_connect_response(sock, deadline)
  470. except Exception:
  471. sock.close()
  472. raise
  473. return sock
  474. T = TypeVar("T")
  475. F = TypeVar("F", bound=Callable[..., T])
  476. class SSLSSLSocket:
  477. """
  478. Socket-like object providing TLS-in-TLS.
  479. Only methods that are used by websockets are implemented.
  480. """
  481. recv_bufsize = 65536
  482. def __init__(
  483. self,
  484. sock: socket.socket,
  485. ssl_context: ssl_module.SSLContext,
  486. server_hostname: str | None = None,
  487. ) -> None:
  488. self.incoming = ssl_module.MemoryBIO()
  489. self.outgoing = ssl_module.MemoryBIO()
  490. self.ssl_socket = sock
  491. self.ssl_object = ssl_context.wrap_bio(
  492. self.incoming,
  493. self.outgoing,
  494. server_hostname=server_hostname,
  495. )
  496. self.run_io(self.ssl_object.do_handshake)
  497. def run_io(self, func: Callable[..., T], *args: Any) -> T:
  498. while True:
  499. want_read = False
  500. want_write = False
  501. try:
  502. result = func(*args)
  503. except ssl_module.SSLWantReadError:
  504. want_read = True
  505. except ssl_module.SSLWantWriteError: # pragma: no cover
  506. want_write = True
  507. # Write outgoing data in all cases.
  508. data = self.outgoing.read()
  509. if data:
  510. self.ssl_socket.sendall(data)
  511. # Read incoming data and retry on SSLWantReadError.
  512. if want_read:
  513. data = self.ssl_socket.recv(self.recv_bufsize)
  514. if data:
  515. self.incoming.write(data)
  516. else:
  517. self.incoming.write_eof()
  518. continue
  519. # Retry after writing outgoing data on SSLWantWriteError.
  520. if want_write: # pragma: no cover
  521. continue
  522. # Return result if no error happened.
  523. return result
  524. def recv(self, buflen: int) -> bytes:
  525. try:
  526. return self.run_io(self.ssl_object.read, buflen)
  527. except ssl_module.SSLEOFError:
  528. return b"" # always ignore ragged EOFs
  529. def send(self, data: BytesLike) -> int:
  530. return self.run_io(self.ssl_object.write, data)
  531. def sendall(self, data: BytesLike) -> None:
  532. # adapted from ssl_module.SSLSocket.sendall()
  533. count = 0
  534. with memoryview(data) as view, view.cast("B") as byte_view:
  535. amount = len(byte_view)
  536. while count < amount:
  537. count += self.send(byte_view[count:])
  538. # recv_into(), recvfrom(), recvfrom_into(), sendto(), unwrap(), and the
  539. # flags argument aren't implemented because websockets doesn't need them.
  540. def __getattr__(self, name: str) -> Any:
  541. return getattr(self.ssl_socket, name)