2828
2929from __future__ import absolute_import
3030
31+ from ipaddress import IPv4Address , IPv4Network
3132from functools import wraps
3233
3334import gevent
@@ -54,11 +55,13 @@ class DnsBlocklist(object):
5455 :meth:`.get()` or ``__contains__``.
5556
5657 :param address: The DNSBL domain name.
58+ :param ignore: List of DNSBL result codes to ignore.
5759
5860 """
5961
60- def __init__ (self , address ):
62+ def __init__ (self , address , ignore = None ):
6163 self .address = address
64+ self .ignore = [IPv4Network (ip ) for ip in (ignore or [])]
6265
6366 def _build_query (self , ip ):
6467 one , two , three , four = ip .split ('.' , 3 )
@@ -86,12 +89,18 @@ def get(self, ip, timeout=None, strict=False):
8689 with gevent .Timeout (timeout , None ):
8790 query = self ._build_query (ip )
8891 try :
89- DNSResolver .query (query , 'A' ).get ()
92+ answers = DNSResolver .query (query , 'A' ).get ()
9093 except DNSError as exc :
9194 if exc .errno == ARES_ENOTFOUND :
9295 return False
9396 logging .log_exception (__name__ , query = query )
9497 else :
98+ if answers :
99+ for rdata in answers :
100+ ip = IPv4Address (rdata .host )
101+ for ignore in self .ignore :
102+ if ip in ignore :
103+ return False
95104 return True
96105 return strict
97106
0 commit comments