Fix dnspr for Windows

(cherry picked from commit a194f207ba)
This commit is contained in:
Vladimir Dubrovin committed 2026-08-26 16:48:09 +03:00
1 parent 3ff0f8b7ab
commit efa6f6560c
7 files changed
+59 -12

No files matched your search

+8 -3
View File
@@ -424,15 +424,20 @@ class Tester:
# ---- DNS ---------------------------------------------------------
def dns_query(self, port, name, host="127.0.0.1"):
"""Ask for an A record and return the addresses in the answer."""
def dns_query(self, port, name, host="127.0.0.1", timeout=2.0):
"""Ask for an A record and return the addresses in the answer.
The default timeout is short: a name server on the loopback answers
at once or not at all, and waiting the full request timeout on every
attempt turns a server that answers nothing into a very slow run.
"""
query = struct.pack("!HHHHHH", 0x2A2A, 0x0100, 1, 0, 0, 0)
for label in name.split("."):
query += bytes([len(label)]) + label.encode()
query += b"\x00" + struct.pack("!HH", 1, 1)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(self.timeout)
sock.settimeout(timeout)
try:
sock.sendto(query, (host, port))
data = sock.recvfrom(65536)[0]