File: ADNS.py

package info (click to toggle)
python-adns 1.2.1-5
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 156 kB
  • ctags: 158
  • sloc: ansic: 972; python: 204; makefile: 2
file content (69 lines) | stat: -rw-r--r-- 2,235 bytes parent folder | download | duplicates (5)
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
"""High-level interface to adns."""

import adns
from exceptions import Exception

class Error(Exception): pass

class QueryEngine:

    callback_submit = None
    callback_submit_reverse = None
    callback_submit_reverse_any = None
    
    def __init__(self, s=None):
        self._s = s or adns.init(adns.iflags.noautosys)
        self._queries = {}

    def synchronous(self, qname, rr, flags=0):
        return self._s.synchronous(qname, rr, flags)

    def submit(self, qname, rr, flags=0, callback=None, extra=None):
        callback = callback or self.callback_submit
        if not callback: raise Error, "callback required"
        q = self._s.submit(qname, rr, flags)
        self._queries[q] = qname, rr, flags, callback, extra

    def submit_reverse(self, qname, rr, flags=0, callback=None, extra=None):
        callback = callback or self.callback_submit_reverse
        if not callback: raise Error, "callback required"
        q = self._s.submit_reverse(qname, rr, flags)
        self._queries[q] = qname, rr, flags, callback, extra

    def submit_reverse_any(self, qname, rr, flags=0,
                           callback=None, extra=None):
        callback = callback or self.callback_submit_reverse_any
        if not callback: raise Error, "callback required"
        q = self._s.submit_reverse_any(qname, rr, flags)
        self._queries[q] = qname, rr, flags, callback, extra

    def cancel(self, query):
        query.cancel()
        try: del self._queries[query]
        except KeyError: pass
        
    def run(self, timeout=0):
        for q in self._s.completed(timeout):
            answer = q.check()
            qname, rr, flags, callback, extra = self._queries[q]
            del self._queries[q]
            apply(callback, (answer, qname, rr, flags, extra))

    def finished(self):
        return not len(self._queries)

    def finish(self):
        while not self.finished():
            self.run(1)

    def run_max(self, max):
        from time import time
        quittime = time()+max
        while not self.finished() and time()<=quittime:
            self.run(1)

    def globalsystemfailure(self):
        self._s.globalsystemfailure()
        self._queries.clear()

init = QueryEngine