diff --git a/apache.py b/apache.py index 123409d..437de35 100755 --- a/apache.py +++ b/apache.py @@ -1,4 +1,5 @@ #!/usr/bin/python +"""Script to create a simple mod_access style list for inclusion within Apache.""" import blocklist import sys @@ -6,19 +7,22 @@ import sys class ApacheBlockList(blocklist.BlockList): """New class that extends the main BlockList class for use with Apache.""" def export(self): - """Exports the blocklist addresses with use within Apache.""" - print "\tOrder deny,allow" - for address in self.data.keys(): - print "\tDeny from %s/32" % address - print "\tAllow from all" + """Exports the blocklist addresses with use within Apache.""" + print "\tOrder deny,allow" + for address in self.data.keys(): + print "\tDeny from %s/32" % address + print "\tAllow from all" -if __name__ == "__main__": - blocklist = ApacheBlockList() +def main(): + """Main program loop.""" + block_list = ApacheBlockList() - blocklist.read("http://server1.example.com/default/blocks.txt") - blocklist.read("http://server2.example.com/default/blocks.txt") + block_list.read("http://server1.example.com/default/blocks.txt") + block_list.read("http://server2.example.com/default/blocks.txt") - blocklist.export() + block_list.export() sys.exit(0) +if __name__ == "__main__": + main() diff --git a/sqlite.py b/sqlite.py index 213718a..8e7095e 100755 --- a/sqlite.py +++ b/sqlite.py @@ -1,10 +1,11 @@ #!/usr/bin/python +"""Script to create a SQLite DB from the blocklist data.""" import blocklist import sys import logging -if sys.version_info >= (2,5): +if sys.version_info >= (2, 5): import sqlite3 else: from pysqlite2 import dbapi2 as sqlite3 @@ -12,30 +13,34 @@ else: class SQLiteBlockList(blocklist.BlockList): """New class to extend the main BlockList class for implementation with Varnish.""" def export(self): - """Exports blocklist criteria to a SQLite file.""" + """Exports blocklist criteria to a SQLite file.""" - try: - db = sqlite3.connect(self.config.get("sqlite", "database")) - cur = db.cursor() + try: + database = sqlite3.connect(self.config.get("sqlite", "database")) + cur = database.cursor() - cur.execute("CREATE TABLE IF NOT EXISTS blocklist(remote_ip VARCHAR(15), forwarded_ip VARCHAR(15), useragent VARCHAR(256), cookie VARCHAR(1024), PRIMARY KEY(remote_ip))") + cur.execute("CREATE TABLE IF NOT EXISTS blocklist(remote_ip VARCHAR(15), forwarded_ip VARCHAR(15), useragent VARCHAR(256), cookie VARCHAR(1024), PRIMARY KEY(remote_ip))") - cur.execute("DELETE FROM blocklist") + cur.execute("DELETE FROM blocklist") - for key,item in self.data.iteritems(): - if item["useragent"] == 'NULL': - item["useragent"] = '(null)' - cur.execute("INSERT INTO blocklist VALUES ('%s', '%s', '%s', '%s')" % (item["remote_ip"], item["forwarded_ip"], item["useragent"], item["cookie"])) - db.commit() - cur.close() - except Exception, e: - logging.error("There was a problem exporting the data to SQLite. %s" % e) + for item in self.data.values(): + if item["useragent"] == 'NULL': + item["useragent"] = '(null)' + cur.execute("INSERT INTO blocklist VALUES ('%s', '%s', '%s', '%s')" % (item["remote_ip"], item["forwarded_ip"], item["useragent"], item["cookie"])) + database.commit() + cur.close() + except sqlite3.Error, error: + logging.error("There was a problem exporting the data to SQLite. %s", error) -if __name__ == "__main__": - blocklist = SQLiteBlockList() +def main(): + """Main program loop.""" + block_list = SQLiteBlockList() - blocklist.read("http://example.com/default/blocks.txt") + block_list.read("http://example.com/default/blocks.txt") - blocklist.export() + block_list.export() sys.exit(0) + +if __name__ == "__main__": + main()