File: client_subnet.py

package info (click to toggle)
python-getdns 1.0.0~b1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 448 kB
  • sloc: ansic: 3,538; python: 607; makefile: 130
file content (36 lines) | stat: -rwxr-xr-x 962 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python

import getdns
from struct import pack, unpack
    

def main():
    CLIENT_SUBNET_OPCODE = 8
        
    address = '192.168.1.0'
    host = 'getdnsapi.net'
    source_len = 12
    
    family = pack("!H", 1)  # start building the edns option fields
    source_len = pack('!B', source_len)
    scope_len = pack('!B', 0)

#
# encoding the binary data in strings makes it really easy
# to build packets by concatenating those strings
#
    address = pack('!BBBB', 192, 168, 1, 0)
    payload = family + source_len + scope_len + address
    ext = { 'add_opt_parameters': {'options':
                                       [ {'option_code': CLIENT_SUBNET_OPCODE,
                                          'option_data': payload} ] }}

    c = getdns.Context()
    c.resolution_type = getdns.RESOLUTION_STUB
    response = c.address(host, extensions=ext)

# do things with response ...
    print response

if __name__ == '__main__':
    main()