Look up a numeric address given a name. Arguments and return values are identical to :func:`socket.getaddrinfo`, except that this version is async. Also, :func:`trio.socket.getaddrinfo` correctly uses IDNA 2008 to process non-ASCII domain names. (:func:`socket.getaddrinfo` uses IDN
(
host: bytes | str | None,
port: bytes | str | int | None,
family: int = 0,
type: int = 0,
proto: int = 0,
flags: int = 0,
)
| 173 | |
| 174 | # It would be possible to @overload the return value depending on Literal[AddressFamily.INET/6], but should probably be added in typeshed first |
| 175 | async def getaddrinfo( |
| 176 | host: bytes | str | None, |
| 177 | port: bytes | str | int | None, |
| 178 | family: int = 0, |
| 179 | type: int = 0, |
| 180 | proto: int = 0, |
| 181 | flags: int = 0, |
| 182 | ) -> list[ |
| 183 | tuple[ |
| 184 | AddressFamily, |
| 185 | SocketKind, |
| 186 | int, |
| 187 | str, |
| 188 | tuple[str, int] | tuple[str, int, int, int] | tuple[int, bytes], |
| 189 | ] |
| 190 | ]: |
| 191 | """Look up a numeric address given a name. |
| 192 | |
| 193 | Arguments and return values are identical to :func:`socket.getaddrinfo`, |
| 194 | except that this version is async. |
| 195 | |
| 196 | Also, :func:`trio.socket.getaddrinfo` correctly uses IDNA 2008 to process |
| 197 | non-ASCII domain names. (:func:`socket.getaddrinfo` uses IDNA 2003, which |
| 198 | can give the wrong result in some cases and cause you to connect to a |
| 199 | different host than the one you intended; see `bpo-17305 |
| 200 | <https://bugs.python.org/issue17305>`__.) |
| 201 | |
| 202 | This function's behavior can be customized using |
| 203 | :func:`set_custom_hostname_resolver`. |
| 204 | |
| 205 | """ |
| 206 | |
| 207 | # If host and port are numeric, then getaddrinfo doesn't block and we can |
| 208 | # skip the whole thread thing, which seems worthwhile. So we try first |
| 209 | # with the _NUMERIC_ONLY flags set, and then only spawn a thread if that |
| 210 | # fails with EAI_NONAME: |
| 211 | def numeric_only_failure(exc: BaseException) -> bool: |
| 212 | return ( |
| 213 | isinstance(exc, _stdlib_socket.gaierror) |
| 214 | and exc.errno == _stdlib_socket.EAI_NONAME |
| 215 | ) |
| 216 | |
| 217 | async with _try_sync(numeric_only_failure): |
| 218 | return _stdlib_socket.getaddrinfo( |
| 219 | host, |
| 220 | port, |
| 221 | family, |
| 222 | type, |
| 223 | proto, |
| 224 | flags | _NUMERIC_ONLY, |
| 225 | ) |
| 226 | # That failed; it's a real hostname. We better use a thread. |
| 227 | # |
| 228 | # Also, it might be a unicode hostname, in which case we want to do our |
| 229 | # own encoding using the idna module, rather than letting Python do |
| 230 | # it. (Python will use the old IDNA 2003 standard, and possibly get the |
| 231 | # wrong answer - see bpo-17305). However, the idna module is picky, and |
| 232 | # will refuse to process some valid hostname strings, like "::1". So if |
no test coverage detected
searching dependent graphs…