''' Created on 04.05.2011 @author: Thomas Fehr, me@monsterli.ch, http://thomasjfehr.monsterli.ch/blog ''' SHOW_DEBUG_MSG = 0 import string import sys import socket ''' @return: 0 If the ip-address is not blacklisted @return: 1 If the ip-address is blacklisted ''' def checkIfBlacklisted( ip, blacklistURL): if SHOW_DEBUG_MSG: print "Checking %s on %s" %(ip,blacklistURL) # turn "1.2.3.4" into "4.3.2.1.DNS_BLACKLIST_DOMAIN" iplist = string.split(ip, ".") iplist.reverse() ip = string.join(iplist, ".") ip += "." + blacklistURL try: #if blacklisted, we receive a IP in a the range 127.0.0.x ret = socket.gethostbyname( ip ) if SHOW_DEBUG_MSG: print "Blacklisted! returned: %s" %ret return 1 except socket.gaierror, message: if SHOW_DEBUG_MSG: print "Not blacklisted [%s]" % message return 0 if __name__ == "__main__": if len(sys.argv) > 2: print checkIfBlacklisted( sys.argv[1] ,sys.argv[2])