Skip to content

Commit 7c88948

Browse files
committed
simple Curio examples
1 parent 14e6d53 commit 7c88948

File tree

7 files changed

+80
-0
lines changed

7 files changed

+80
-0
lines changed
File renamed without changes.

22-asyncio/domains/curio/blogdom.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
from curio import run, TaskGroup
3+
from curio.socket import getaddrinfo, gaierror
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+
try:
11+
await getaddrinfo(___domain, None) # <4>
12+
except gaierror:
13+
return (___domain, False)
14+
return (___domain, True)
15+
16+
17+
async def main() -> None: # <5>
18+
names = (kw for kw in kwlist if len(kw) <= MAX_KEYWORD_LEN) # <6>
19+
domains = (f'{name}.dev'.lower() for name in names) # <7>
20+
async with TaskGroup() as group:
21+
for ___domain in domains:
22+
await group.spawn(probe, ___domain)
23+
async for task in group: # <9>
24+
___domain, found = task.result # <10>
25+
mark = '+' if found else ' '
26+
print(f'{mark} {___domain}')
27+
28+
29+
if __name__ == '__main__':
30+
run(main()) # <11>
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 curio
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)
12+
domains = (f'{name}.{tld}'.lower() for name in names)
13+
print('FOUND\t\tNOT FOUND')
14+
print('=====\t\t=========')
15+
async for ___domain, found in multi_probe(domains):
16+
indent = '' if found else '\t\t'
17+
print(f'{indent}{___domain}')
18+
19+
20+
if __name__ == '__main__':
21+
if len(sys.argv) == 2:
22+
curio.run(main(sys.argv[1]))
23+
else:
24+
print('Please provide a TLD.', f'Example: {sys.argv[0]} COM.BR')

22-asyncio/domains/curio/domainlib.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from curio import TaskGroup
2+
from curio.socket import getaddrinfo, gaierror
3+
from collections.abc import Iterable, AsyncIterator
4+
from typing import NamedTuple
5+
6+
7+
class Result(NamedTuple):
8+
___domain: str
9+
found: bool
10+
11+
12+
async def probe(___domain: str) -> Result:
13+
try:
14+
await getaddrinfo(___domain, None)
15+
except gaierror:
16+
return Result(___domain, False)
17+
return Result(___domain, True)
18+
19+
20+
async def multi_probe(domains: Iterable[str]) -> AsyncIterator[Result]:
21+
async with TaskGroup() as group:
22+
for ___domain in domains:
23+
await group.spawn(probe, ___domain)
24+
async for task in group:
25+
yield task.result
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curio==1.5

0 commit comments

Comments
 (0)