Skip to content

Commit

Permalink
Add support for getaddrinfo
Browse files Browse the repository at this point in the history
fixes #23
  • Loading branch information
bdraco committed Mar 26, 2024
1 parent d40f913 commit 9efd0dd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions aiodns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ def gethostbyname(self, host: str, family: socket.AddressFamily) -> asyncio.Futu
cb = functools.partial(self._callback, fut)
self._channel.gethostbyname(host, family, cb)
return fut

def getaddrinfo(self, host: str, family: socket.AddressFamily = 0, port: Optional[int] = None, proto: int = 0, type: int = 0, flags: int = 0) -> asyncio.Future:
fut = asyncio.Future(loop=self.loop) # type: asyncio.Future
cb = functools.partial(self._callback, fut)
self._channel.getaddrinfo(host, port, cb, family=family, type=type, proto=proto, flags=flags)
return fut

def gethostbyaddr(self, name: str) -> asyncio.Future:
fut = asyncio.Future(loop=self.loop) # type: asyncio.Future
Expand Down
20 changes: 20 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,26 @@ def test_gethostbyname(self):
result = self.loop.run_until_complete(f)
self.assertTrue(result)

def test_getaddrinfo_address_family_0(self):
f = self.resolver.getaddrinfo('google.com')
result = self.loop.run_until_complete(f)
self.assertTrue(result)
self.assertTrue(len(result.nodes) > 1)

def test_getaddrinfo_address_family_af_inet(self):
f = self.resolver.getaddrinfo('google.com', socket.AF_INET)
result = self.loop.run_until_complete(f)
self.assertTrue(result)
self.assertTrue(len(result.nodes) == 1)
self.assertTrue(result.nodes[0].family == socket.AF_INET)

def test_getaddrinfo_address_family_af_inet6(self):
f = self.resolver.getaddrinfo('google.com', socket.AF_INET6)
result = self.loop.run_until_complete(f)
self.assertTrue(result)
self.assertTrue(len(result.nodes) == 1)
self.assertTrue(result.nodes[0].family == socket.AF_INET6)

@unittest.skipIf(sys.platform == 'win32', 'skipped on Windows')
def test_gethostbyaddr(self):
f = self.resolver.gethostbyaddr('127.0.0.1')
Expand Down

0 comments on commit 9efd0dd

Please sign in to comment.