Skip to content

Instantly share code, notes, and snippets.

@stucka
Created April 11, 2016 13:41
Show Gist options
  • Save stucka/75b7bf554e2b131f48e25f1f986af2ce to your computer and use it in GitHub Desktop.
Save stucka/75b7bf554e2b131f48e25f1f986af2ce to your computer and use it in GitHub Desktop.
Look for local IP blocks in Maxmind data
import csv
# data source is here https://dev.maxmind.com/geoip/geoip2/geoip2-csv-databases/
# lat column = -2
# long column = -1
# Set up bounding box in a very bad way
minlat = 26.3
maxlat = 27.52
minlong = -80.9
maxlong = -79.95
with open('local.csv', 'wb') as outfile:
put = csv.writer(outfile)
with open('GeoLite2-City-Blocks-IPv4.csv', 'rb') as infile:
sourcecsv = csv.reader(infile)
headers = sourcecsv.next()
headers.append("latlong")
put.writerow(headers)
for myrow in sourcecsv:
try: # If lat or long is mangled, it won't convert to a float, so we'll give it a bad value
mylat = float(myrow[-2])
except ValueError:
mylat = -181
try:
mylong = float(myrow[-1])
except ValueError:
mylong = -181
if mylat >= minlat and mylat <= maxlat and mylong >= minlong and mylong <= maxlong:
myrow.append(myrow[-2] + ", " + myrow[-1]) # append "lat, long"
put.writerow(myrow)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment