Last active
April 23, 2018 15:57
-
-
Save subuk/57e5a5adcdb53db6632d26131f35096a to your computer and use it in GitHub Desktop.
This file contains hidden or 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/env python3 | |
import csv | |
import codecs | |
import urllib.request | |
import argparse | |
import ipaddress | |
def iter_blocked_subnets(): | |
url = "https://raw.githubusercontent.com/zapret-info/z-i/master/dump.csv" | |
response = urllib.request.urlopen(url) | |
reader = csv.reader(codecs.iterdecode(response, 'cp1251'), delimiter=';') | |
for row in reader: | |
for ip in row[0].split(u' | '): | |
try: | |
yield ipaddress.ip_network(ip.strip()) | |
except (ipaddress.AddressValueError, ValueError): | |
pass | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("ip") | |
args = parser.parse_args() | |
ip = ipaddress.ip_address(args.ip) | |
for network in iter_blocked_subnets(): | |
if ip in network: | |
print("YES. %s" % network) | |
return | |
print("NO") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment