Debian Codegraph @

examples/receive_notify.py

raw
1 #!/usr/bin/env python3
2
3 # This is just a toy, real code would check that the received message
4 # really was a NOTIFY, and otherwise handle errors.
5
6 import socket
7 from typing import cast
8
9 import dns.flags
10 import dns.message
11 import dns.name
12 import dns.rdataclass
13 import dns.rdatatype
14
15 address = "127.0.0.1"
16 port = 53535
17
18 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
19 s.bind((address, port))
20 while True:
21 (wire, address) = s.recvfrom(512)
22 notify = dns.message.from_wire(wire)
23
24 try:
25 soa = notify.find_rrset(
26 notify.answer, notify.question[0].name, dns.rdataclass.IN, dns.rdatatype.SOA
27 )
28
29 # Do something with the SOA RR here
30 print("The serial number for", soa.name, "is", soa[0].serial)
31 except KeyError:
32 # No SOA RR in the answer section.
33 pass
34
35 response = dns.message.make_response(notify) # type: dns.message.Message
36 response.flags |= dns.flags.AA
37 wire = response.to_wire(cast(dns.name.Name, response))
38 s.sendto(wire, address)