Skip to content

Instantly share code, notes, and snippets.

@lpereira
Last active November 29, 2017 00:08
Show Gist options
  • Save lpereira/c10a6ad714c1f3de3f42 to your computer and use it in GitHub Desktop.
Save lpereira/c10a6ad714c1f3de3f42 to your computer and use it in GitHub Desktop.
Block ads and other annoyances by redirecting their host names to 127.0.0.1
#!/usr/bin/python
import requests
sources = [
'http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext',
'http://hosts-file.net/.%5Cad_servers.txt',
'https://adaway.org/hosts.txt',
'http://winhelp2002.mvps.org/hosts.txt',
'http://sysctl.org/cameleon/hosts',
'http://someonewhocares.org/hosts/hosts'
]
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
}
hosts = set()
hostnames = 0
for source in sources:
print('# Fetching %s...' % source)
request = requests.get(source, headers=headers)
print('# Parsing %s...' % source)
for line in (l.strip() for l in request.text.split('\n')):
if not line.startswith('127.0.0.1'):
continue
if '#' in line:
line = line.split('#', 2)[0].strip()
ip_address, hostname = line.split()
hostnames += 1
hosts.add(hostname)
print('# %d hostnames, %d unique' % (hostnames, len(hosts)))
for hostname in sorted(hosts):
print('127.0.0.1 %s' % hostname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment