Skip to content

Commit 1d1caa2

Browse files
committed
udnspkt: Add example.
1 parent 7e33441 commit 1d1caa2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

udnspkt/example_resolve.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import uio
2+
import usocket
3+
4+
import udnspkt
5+
6+
7+
s = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM)
8+
dns_addr = usocket.getaddrinfo("127.0.0.1", 53)[0][-1]
9+
10+
11+
def resolve(domain, is_ipv6):
12+
buf = uio.BytesIO(48)
13+
udnspkt.make_req(buf, "google.com", is_ipv6)
14+
v = buf.getvalue()
15+
print("query: ", v)
16+
s.sendto(v, dns_addr)
17+
18+
resp = s.recv(1024)
19+
print("resp:", resp)
20+
buf = uio.BytesIO(resp)
21+
22+
addr = udnspkt.parse_resp(buf, is_ipv6)
23+
print("bin addr:", addr)
24+
print("addr:", usocket.inet_ntop(usocket.AF_INET6 if is_ipv6 else usocket.AF_INET, addr))
25+
26+
27+
resolve("google.com", False)
28+
print()
29+
resolve("google.com", True)

0 commit comments

Comments
 (0)