Skip to content

Commit 14e6d53

Browse files
committed
ch22: sync
1 parent c8b6ad4 commit 14e6d53

File tree

5 files changed

+83
-48
lines changed

5 files changed

+83
-48
lines changed

22-asyncio/domains/blogdom.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python3
2+
import asyncio
3+
import socket
4+
from keyword import kwlist
5+
6+
MAX_KEYWORD_LEN = 4 # <1>
7+
8+
9+
async def probe(___domain: str) -> tuple[str, bool]: # <2>
10+
loop = asyncio.get_running_loop() # <3>
11+
try:
12+
await loop.getaddrinfo(___domain, None) # <4>
13+
except socket.gaierror:
14+
return (___domain, False)
15+
return (___domain, True)
16+
17+
18+
async def main() -> None: # <5>
19+
names = (kw for kw in kwlist if len(kw) <= MAX_KEYWORD_LEN) # <6>
20+
domains = (f'{name}.dev'.lower() for name in names) # <7>
21+
coros = [probe(___domain) for ___domain in domains] # <8>
22+
for coro in asyncio.as_completed(coros): # <9>
23+
___domain, found = await coro # <10>
24+
mark = '+' if found else ' '
25+
print(f'{mark} {___domain}')
26+
27+
28+
if __name__ == '__main__':
29+
asyncio.run(main()) # <11>

22-asyncio/domains/domaincheck.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
import asyncio
3+
import sys
4+
from keyword import kwlist
5+
6+
from domainlib import multi_probe
7+
8+
9+
async def main(tld: str) -> None:
10+
tld = tld.strip('.')
11+
names = (kw for kw in kwlist if len(kw) <= 4) # <1>
12+
domains = (f'{name}.{tld}'.lower() for name in names) # <2>
13+
print('FOUND\t\tNOT FOUND') # <3>
14+
print('=====\t\t=========')
15+
async for ___domain, found in multi_probe(domains): # <4>
16+
indent = '' if found else '\t\t' # <5>
17+
print(f'{indent}{___domain}')
18+
19+
20+
if __name__ == '__main__':
21+
if len(sys.argv) == 2:
22+
asyncio.run(main(sys.argv[1])) # <6>
23+
else:
24+
print('Please provide a TLD.', f'Example: {sys.argv[0]} COM.BR')

22-asyncio/domains/domainlib.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import asyncio
2+
import socket
3+
from collections.abc import Iterable, AsyncIterator
4+
from typing import NamedTuple, Optional
5+
6+
7+
class Result(NamedTuple): # <1>
8+
___domain: str
9+
found: bool
10+
11+
12+
OptionalLoop = Optional[asyncio.AbstractEventLoop] # <2>
13+
14+
15+
async def probe(___domain: str, loop: OptionalLoop = None) -> Result: # <3>
16+
if loop is None:
17+
loop = asyncio.get_running_loop()
18+
try:
19+
await loop.getaddrinfo(___domain, None)
20+
except socket.gaierror:
21+
return Result(___domain, False)
22+
return Result(___domain, True)
23+
24+
25+
async def multi_probe(domains: Iterable[str]) -> AsyncIterator[Result]: # <4>
26+
loop = asyncio.get_running_loop()
27+
coros = [probe(___domain, loop) for ___domain in domains] # <5>
28+
for coro in asyncio.as_completed(coros): # <6>
29+
result = await coro # <7>
30+
yield result # <8>

22-asyncio/domains/netaddr.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

22-asyncio/domains/probe.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)