#!/usr/bin/python2.2 import socket import string debug = 0 # returns 1 if ip address is in the blacklist def blacklisted( ip ): if debug: print "Checking %s" % ip # turn "1.2.3.4" into "4.3.2.1.sbl-xbl.spamhaus.org" iplist = string.split(ip, ".") iplist.reverse() ip = string.join(iplist, ".") ip += ".sbl-xbl.spamhaus.org" try: socket.gethostbyname( ip ) if debug: print "Blacklisted!" return 1 except socket.gaierror, message: if debug: print "Not blacklisted [%s]" % message return 0 if __name__ == "__main__": import sys debug = 1 if len(sys.argv) > 1: blacklisted( sys.argv[1] ) else: # localhost, shouldn't be blacklisted if blacklisted("127.0.0.1") != 0: print "Test 1 failed" # test address, should be blacklisted if blacklisted("127.0.0.2") != 1: print "Test 2 failed" # a genuine scrote, should be blacklisted # if only this test fails, check with spamhaus # there's always a slim chance the box has been sorted if blacklisted("213.56.68.29") != 1: print "Test 3 failed" # feetup.org - should not be blacklisted! if blacklisted("69.73.148.126") != 0: print "Test 4 failed"