Skip to content

Listen on custom address in AsyncHTTPTestCase #2969

Closed

Description

@chrishaig14

I need to run a test and listen on 0.0.0.0 to allow an external service to connect through another IP, but the default one is 127.0.0.1.

In testing.py:

def bind_unused_port(reuse_port: bool = False) -> Tuple[socket.socket, int]:
    """Binds a server socket to an available port on localhost.

    Returns a tuple (socket, port).

    .. versionchanged:: 4.4
       Always binds to ``127.0.0.1`` without resolving the name
       ``localhost``.
    """
    sock = netutil.bind_sockets(
        0, "127.0.0.1", family=socket.AF_INET, reuse_port=reuse_port
    )[0]
    port = sock.getsockname()[1]
    return sock, port

Is there a clean way of listening on 0.0.0.0 in a test?

Activity

  1. bdarnell commented on Jan 9, 2021

    @bdarnell
    Member

    Not currently. It's a three-line method so you could just copy it and modify as needed. Or I wouldn't mind a PR adding a keyword argument to control the address.

  2. youguanxinqing commented on Jan 11, 2021

    @youguanxinqing
    Contributor
  3. added a commit that references this issue on Feb 4, 2021
  4. bdarnell commented on Feb 4, 2021

    @bdarnell
    Member

    Fixed in #2974

  5. added a commit that references this issue on Apr 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions