Skip to content

Instantly share code, notes, and snippets.

@RyanHarijanto
Created October 3, 2015 10:00
Show Gist options
  • Save RyanHarijanto/7a1f1616b385500a7b11 to your computer and use it in GitHub Desktop.
Save RyanHarijanto/7a1f1616b385500a7b11 to your computer and use it in GitHub Desktop.
BlockThem Python2 Example
#!/usr/bin/python
import urllib2
import json
import re
def isDomainBlockedLive( domain, jsonFile ):
domain = re.sub(r'http(s?):\/\/', '', domain)
domain = re.sub(r'^www\.', '', domain)
domain = re.sub(r'\/$', '', domain)
headers = { 'User-Agent' : 'Mozilla/5.0' }
req = urllib2.Request(jsonFile, None, headers)
res = urllib2.urlopen(req).read()
data = json.loads(res)
return domain in data
def isDomainBlockedCached( domain, jsonFile ):
domain = re.sub(r'http(s?):\/\/', '', domain)
domain = re.sub(r'^www\.', '', domain)
domain = re.sub(r'\/$', '', domain)
with open (jsonFile, "r") as myfile:
res=myfile.read()
data = json.loads(res)
return domain in data
# Example: Live
# Output: False
print isDomainBlockedLive( 'gmail.com', 'http://api.blockthem.io/v1/blacklist.json' )
# Example: Cached
# Output: True
print isDomainBlockedCached( 'mailinator.com', 'blacklist.json' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment