-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdns_example.py
More file actions
44 lines (35 loc) · 1017 Bytes
/
Copy pathdns_example.py
File metadata and controls
44 lines (35 loc) · 1017 Bytes
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
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import dns.resolver
__RESOLV = dns.resolver.Resolver() #init, setting server ip
__RESOLV.nameservers = ["8.8.8.8"]
def get(dom, rdtype):
'''
param dom [string] domain, such as 'www.baidu.com'
param rdtype [int] record type, such as 'A'/1...
return [list] dns resolve result
'''
try:
answer = __RESOLV.query(dom, rdtype) #class Answer
except (dns.resolver.NoNameservers,dns.resolver.NoAnswer) as err:
print(err)
return []
ret = []
for reses in answer.response.answer: #class RRset
for res in reses: #class A/CNAME/...
if res.rdtype != rdtype:
continue
ret.append(res.to_text())
return ret
if __name__ == "__main__":
type_val = {
"A":1,
"NS":2,
"CNAME":5,
"SOA":6,
"PTR":12,
"MX":15,
"TXT":16,
"AAAA":28,
}
print(get('baidu.com', type_val.get('A')))