1717
1818class DNSQuery (object ):
1919 """
20- Used for making fake DNS resolution responses based on received
21- raw request
22-
23- Reference(s):
24- http://code.activestate.com/recipes/491264-mini-fake-dns-server/
25- https://code.google.com/p/marlon-tools/source/browse/tools/dnsproxy/dnsproxy.py
26-
2720 >>> DNSQuery(b'|K\\ x01 \\ x00\\ x01\\ x00\\ x00\\ x00\\ x00\\ x00\\ x01\\ x03www\\ x06google\\ x03com\\ x00\\ x00\\ x01\\ x00\\ x01\\ x00\\ x00)\\ x10\\ x00\\ x00\\ x00\\ x00\\ x00\\ x00\\ x0c\\ x00\\ n\\ x00\\ x08O4|Np!\\ x1d\\ xb3')._query == b"www.google.com."
2821 True
2922 """
@@ -32,9 +25,9 @@ def __init__(self, raw):
3225 self ._raw = raw
3326 self ._query = b""
3427
35- type_ = (ord (raw [2 :3 ]) >> 3 ) & 15 # Opcode bits
28+ type_ = (ord (raw [2 :3 ]) >> 3 ) & 15 # Opcode bits
3629
37- if type_ == 0 : # Standard query
30+ if type_ == 0 : # Standard query
3831 i = 12
3932 j = ord (raw [i :i + 1 ])
4033
@@ -65,6 +58,15 @@ def response(self, resolution):
6558 return retVal
6659
6760class DNSServer (object ):
61+ """
62+ Used for making fake DNS resolution responses based on received
63+ raw request
64+
65+ Reference(s):
66+ http://code.activestate.com/recipes/491264-mini-fake-dns-server/
67+ https://code.google.com/p/marlon-tools/source/browse/tools/dnsproxy/dnsproxy.py
68+ """
69+
6870 def __init__ (self ):
6971 self ._check_localhost ()
7072 self ._requests = []
@@ -99,9 +101,15 @@ def pop(self, prefix=None, suffix=None):
99101
100102 retVal = None
101103
104+ if prefix and hasattr (prefix , "encode" ):
105+ prefix = prefix .encode ()
106+
107+ if suffix and hasattr (suffix , "encode" ):
108+ suffix = suffix .encode ()
109+
102110 with self ._lock :
103111 for _ in self ._requests :
104- if prefix is None and suffix is None or re .search (r "%s\..+\.%s" % (prefix , suffix ), _ , re .I ):
112+ if prefix is None and suffix is None or re .search (b "%s\..+\.%s" % (prefix , suffix ), _ , re .I ):
105113 retVal = _
106114 self ._requests .remove (_ )
107115 break
0 commit comments