DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Build Blacklist .htaccess File Modified

04.09.2005
| 5160 views |
  • submit to reddit
        
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
blacklist_builder.py
Get the entries from a blacklist
and print them to standard output
"""
import urllib2, re
latest = urllib2.urlopen("http://www.unknowngenius.com/blog/blacklist/")
prefix = 'RewriteEngine  on'
suffix = 'RewriteRule /(.*) / [F,L]'
str = [prefix]
begin = "RewriteCond %{HTTP_REFERER} ^.*"
for line in latest:
	print '.',
	str.append(begin + re.sub('(\s#:url:|\s#:regex-url:|\s#:rbl:)', ".*$ [OR,NC]", line))
str.append(suffix)
file('.htaccess', 'w').write('\n'.join(str))


Use of list to speed up.

Added prefix and suffix to make it a complete rewrite.