ch22: examples

This commit is contained in:
Luciano Ramalho 2021-02-28 22:09:40 -03:00
parent 98b8cfd068
commit 4dcb6aadf4
2 changed files with 9 additions and 9 deletions

View File

@ -5,21 +5,21 @@ from typing import NamedTuple
class Result(NamedTuple):
name: str
domain: str
found: bool
async def probe(loop: asyncio.AbstractEventLoop, name: str) -> Result:
async def probe(loop: asyncio.AbstractEventLoop, domain: str) -> Result:
try:
await loop.getaddrinfo(name, None)
await loop.getaddrinfo(domain, None)
except socket.gaierror:
return Result(name, False)
return Result(name, True)
return Result(domain, False)
return Result(domain, True)
async def multi_probe(names: Iterable[str]) -> AsyncIterator[Result]:
async def multi_probe(domains: Iterable[str]) -> AsyncIterator[Result]:
loop = asyncio.get_running_loop()
coros = [probe(loop, name) for name in names]
coros = [probe(loop, domain) for domain in domains]
for coro in asyncio.as_completed(coros):
result = await coro
yield result

View File

@ -11,9 +11,9 @@ async def main(tld: str) -> None:
tld = tld.strip('.')
names = (kw for kw in kwlist if len(kw) <= 4)
domains = (f'{name}.{tld}'.lower() for name in names)
async for name, found in multi_probe(domains):
async for domain, found in multi_probe(domains):
mark = '.' if found else '?\t\t'
print(f'{mark} {name}')
print(f'{mark} {domain}')
if __name__ == '__main__':