micropython-lib/micropython/udnspkt/example_resolve.py

30 wiersze
637 B
Python

import io
import socket
2017-12-17 14:29:38 +00:00
import udnspkt
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
dns_addr = socket.getaddrinfo("127.0.0.1", 53)[0][-1]
2017-12-17 14:29:38 +00:00
def resolve(domain, is_ipv6):
buf = io.BytesIO(48)
2017-12-17 14:29:38 +00:00
udnspkt.make_req(buf, "google.com", is_ipv6)
v = buf.getvalue()
print("query: ", v)
s.sendto(v, dns_addr)
resp = s.recv(1024)
print("resp:", resp)
buf = io.BytesIO(resp)
2017-12-17 14:29:38 +00:00
addr = udnspkt.parse_resp(buf, is_ipv6)
print("bin addr:", addr)
print("addr:", socket.inet_ntop(socket.AF_INET6 if is_ipv6 else socket.AF_INET, addr))
2017-12-17 14:29:38 +00:00
resolve("google.com", False)
print()
resolve("google.com", True)