forked from pkrumins/adns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
readme.txt
executable file
·126 lines (81 loc) · 4.29 KB
/
readme.txt
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
This is a transition of a small, but useful python library to python 3.
Transition was made by Alpherie ([email protected])
REMEMBER! This code requiers python3-adns to work!
You can get it, for examle, here:
https://github.com/trolldbois/python3-adns/
Further goes original readme from the previous author.
------------------------------------------------------------------------------
This is a tiny Python library for resolving a lot of DNS records
asynchronously. On a speedy internet connection it managed to do
20,000 resolutions per minute.
It was written by Peteris Krumins ([email protected]).
His blog is at http://www.catonmat.net -- good coders code, great reuse.
The code is licensed under the MIT license.
The code was written as a part of the article "Resolving DNS Asynchronously"
on my website. The whole article can be read at:
http://www.catonmat.net/blog/asynchronous-dns-resolution/
------------------------------------------------------------------------------
Table of contents:
[1] The problem.
[2] The solution and how to use async_dns.py
[3] What is slow_dns.py included in this source tree?
[1]-The-problem---------------------------------------------------------------
Once upon a time, I had to quickly resolve thousands of DNS names. My first
solution was to call gethostbyname repeatedly for each of the hosts. This
turned out to be extremely slow. I could only do 200 hosts in a minute. I
talked with someone and he suggested to try to do it asynchronously. I looked
around and found adns - asynchronous dns library written in C (see link (1)
below). Since I was writing the code in Python, I looked around some more and
found Python bindings for adns (see link (2) below). I tried adns and - wow -
I could do 20,000 hosts in a minute!
...
Continue reading on http://www.catonmat.net/blog/asynchronous-dns-resolution/
Link references:
(1) http://www.chiark.greenend.org.uk/~ian/adns/
(2) http://code.google.com/p/adns-python/
[2]-The-solution--------------------------------------------------------------
I wrote async_dns.py Python module to do the resolution. It uses Python
wrapper for adns C library (see links (1) and (2) above).
The async_dns.py module exports AsyncResolver class. The constructor of this
class takes a list of domains to resolve.
Here is an example that resolves three domains asynchronously:
from async_dns import AsyncResolver
ar = AsyncResolver(
["www.google.com", "www.reddit.com", "www.nonexistz.net"]
)
resolved = ar.resolve()
for host, ip in resolved.items():
if ip is None:
print "%s could not be resolved." % host
else:
print "%s resolved to %s" % (host, ip)
The constructor of Asynchronously also takes 'intensity' paramter, which
specifies how many hosts to resolve asynchronously at once.
Calling the resolve() member function on an AsyncResolver object starts
resolving all the domains that were passed to AsyncResolver constructor.
Once it's done, it returns a dictionary of { 'host': 'ip' } structure.
[3]-What-is-slow_dns.py-in-the-source-tree?-----------------------------------
The file slow_dns.py is my first attempt at resolving a lot of DNS hosts. It
uses the standard system resolver "gethostbyname" from Python's socket module.
It can only do 200 resolutions per minute.
Python module slow_dns.py exports resolve_slow function, it takes a list of
hosts as an argument, resolves them, and returns a dictionary of
{ 'host': 'ip' } pairs.
You can use it the following way:
from slow_dns import resolve_slow
resolved = resolve_slow(['www.reddit.com', 'www.catonmat.net'])
for host, ip in resolved.items():
if ip is None:
print "%s could not be resolved." % host
else:
print "%s resolved to %s" % (host, ip)
------------------------------------------------------------------------------
See my original article for more information on both async_dns.py and
slow_dns.py.
It's here:
http://www.catonmat.net/blog/asynchronous-dns-resolution/
------------------------------------------------------------------------------
That's it. Happy asynchronous resolution!
Sincerely,
Peteris Krumins
http://www.catonmat.net