Created
October 3, 2015 10:00
-
-
Save RyanHarijanto/7a1f1616b385500a7b11 to your computer and use it in GitHub Desktop.
BlockThem Python2 Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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