Skip to content

Passing custom sock to client (MPTCP and other socket flags) #1587

@Pithikos

Description

@Pithikos

Currently it's possible to pass a custom socket object on the server side. This is handy in my case since we want to use Multi-Path TCP (MPTCP).

async def on_connection(*args, **kwargs):
    print("Connected")

def create_mptcp_sock(server_ip, server_port):
    """
    Create MultiPath TCP socket
    """
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_MPTCP)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.setblocking(False)
    sock.bind((server_ip, server_port))
    sock.listen(0)
    return sock

sock = create_mptcp_sock(server_ip, server_port)
async with websockets.serve(on_connection, sock=sock) as server:
    ...

However when I pass a sock similarly in the client side, I get an exception when the client tries to connect.

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_MPTCP)
sock.setblocking(False)

async with websockets.connect(uri, sock=sock) as ws:
    ....

Traceback:

Traceback (most recent call last):
  File "/home/alex/workspace/projectx/.venv/lib/python3.12/site-packages/websockets/legacy/client.py", line 139, in read_http_response
    status_code, reason, headers = await read_response(self.reader)
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/workspace/projectx/.venv/lib/python3.12/site-packages/websockets/legacy/http.py", line 120, in read_response
    status_line = await read_line(stream)
                  ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/workspace/projectx/.venv/lib/python3.12/site-packages/websockets/legacy/http.py", line 194, in read_line
    line = await stream.readline()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/streams.py", line 568, in readline
    line = await self.readuntil(sep)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/streams.py", line 660, in readuntil
    await self._wait_for_data('readuntil')
  File "/usr/lib/python3.12/asyncio/streams.py", line 545, in _wait_for_data
    await self._waiter
  File "/usr/lib/python3.12/asyncio/selector_events.py", line 1075, in write
    n = self._sock.send(data)
        ^^^^^^^^^^^^^^^^^^^^^
BrokenPipeError: [Errno 32] Broken pipe

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/alex/workspace/projectx/controller/controller.py", line 819, in <module>
    asyncio.run(connection_loop(state))
  File "/usr/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/alex/workspace/projectx/controller/controller.py", line 729, in connection_loop
    async with websockets.connect(
  File "/home/alex/workspace/projectx/.venv/lib/python3.12/site-packages/websockets/legacy/client.py", line 629, in __aenter__
    return await self
           ^^^^^^^^^^
  File "/home/alex/workspace/projectx/.venv/lib/python3.12/site-packages/websockets/legacy/client.py", line 647, in __await_impl_timeout__
    return await self.__await_impl__()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/workspace/projectx/.venv/lib/python3.12/site-packages/websockets/legacy/client.py", line 654, in __await_impl__
    await protocol.handshake(
  File "/home/alex/workspace/projectx/.venv/lib/python3.12/site-packages/websockets/legacy/client.py", line 319, in handshake
    status_code, response_headers = await self.read_http_response()
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/workspace/projectx/.venv/lib/python3.12/site-packages/websockets/legacy/client.py", line 141, in read_http_response
    raise InvalidMessage("did not receive a valid HTTP response") from exc
websockets.exceptions.InvalidMessage: did not receive a valid HTTP response

Is there a way to achieve this with the current library?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions