From 8f38fc27245a67fa0061b011c066f1ace7fb407f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 29 Mar 2024 15:56:22 -1000 Subject: [PATCH] Add support for getnameinfo This is a followup to #118 to add `getnameinfo` as well to support https://github.com/aio-libs/aiohttp/pull/8270 --- aiodns/__init__.py | 10 +++++++++- tests.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/aiodns/__init__.py b/aiodns/__init__.py index cb5c77c..88f0ca8 100644 --- a/aiodns/__init__.py +++ b/aiodns/__init__.py @@ -9,7 +9,9 @@ Any, Optional, Set, - Sequence + Sequence, + Tuple, + Union ) from . import error @@ -117,6 +119,12 @@ def getaddrinfo(self, host: str, family: socket.AddressFamily = socket.AF_UNSPEC self._channel.getaddrinfo(host, port, cb, family=family, type=type, proto=proto, flags=flags) return fut + def getnameinfo(self, sockaddr: Union[Tuple[str, int], Tuple[str, int, int, int]], flags: int = 0) -> asyncio.Future: + fut = asyncio.Future(loop=self.loop) # type: asyncio.Future + cb = functools.partial(self._callback, fut) + self._channel.getnameinfo(sockaddr, flags, cb) + return fut + def gethostbyaddr(self, name: str) -> asyncio.Future: fut = asyncio.Future(loop=self.loop) # type: asyncio.Future cb = functools.partial(self._callback, fut) diff --git a/tests.py b/tests.py index bf1bdb9..06f45f1 100755 --- a/tests.py +++ b/tests.py @@ -169,6 +169,18 @@ def test_getaddrinfo_address_family_af_inet6(self): self.assertTrue(result) self.assertTrue(all(node.family == socket.AF_INET6 for node in result.nodes)) + def test_getnameinfo_ipv4(self): + f = self.resolver.getnameinfo(('127.0.0.1', 0)) + result = self.loop.run_until_complete(f) + self.assertTrue(result) + self.assertTrue(result.node) + + def test_getnameinfo_ipv6(self): + f = self.resolver.getnameinfo(('::1', 0, 0, 0)) + result = self.loop.run_until_complete(f) + self.assertTrue(result) + self.assertTrue(result.node) + @unittest.skipIf(sys.platform == 'win32', 'skipped on Windows') def test_gethostbyaddr(self): f = self.resolver.gethostbyaddr('127.0.0.1')