]>
Commit | Line | Data |
---|---|---|
440aba92 MT |
1 | #!/usr/bin/python |
2 | ||
3 | import ipaddress | |
4 | import pycares | |
5 | import tornado.gen | |
6 | import tornado.platform.caresresolver | |
7 | ||
8 | class Resolver(tornado.platform.caresresolver.CaresResolver): | |
9 | def initialize(self, **kwargs): | |
10 | super().initialize() | |
11 | ||
12 | # Overwrite Channel | |
13 | self.channel = pycares.Channel(sock_state_cb=self._sock_state_cb, **kwargs) | |
14 | ||
15 | async def query(self, name, type=pycares.QUERY_TYPE_A): | |
16 | # Create a new Future | |
17 | fut = tornado.gen.Future() | |
18 | ||
19 | # Perform the query | |
20 | self.channel.query(name, type, lambda result, error: fut.set_result((result, error))) | |
21 | ||
22 | # Wait for the response | |
23 | result, error = await fut | |
24 | ||
25 | # Handle any errors | |
26 | if error: | |
27 | # NXDOMAIN | |
28 | if error == pycares.errno.ARES_ENOTFOUND: | |
29 | return | |
30 | ||
31 | # Ignore responses with no data | |
32 | elif error == pycares.errno.ARES_ENODATA: | |
33 | return | |
34 | ||
35 | raise IOError( | |
36 | "C-Ares returned error %s: %s while resolving %s" | |
37 | % (error, pycares.errno.strerror(error), name) | |
38 | ) | |
39 | ||
40 | # Return the result | |
41 | return result |