Skip to content

Why resolve hostname -> IP through a connect rather than DNS? #2

@tgwizard

Description

@tgwizard

Thank you for this library, it looks almost exactly like what I need.

In

async def _resolve(self, request: Request, hostname: str, port: int) -> str:
try:
with fail_after(self.connect_timeout):
async with await connect_tcp(
hostname,
port,
happy_eyeballs_delay=self.happy_eyeballs_delay,
) as stream:
return stream._raw_socket.getpeername()[0]
except TimeoutError as e:
raise ConnectTimeout(str(e), request=request) from e
except OSError as e:
raise ConnectError(str(e), request=request) from e

you open a TCP connection to the host just to get the IP. Why not do something like

socket.getaddrinfo(host, port, type=socket.SOCK_STREAM)

to get the IP?

The drawback I see with the connection is that you have to establish a full connection, just to discard it immediately. The benefit of doing a DNS lookup is that you always have to do that anyway, and you can also validate all the IPs that the domain resolves too.

Happy to attempt a PR if you agree.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions