Skip to content

Instantly share code, notes, and snippets.

@zachradtka
Created August 25, 2015 19:33
Show Gist options
  • Save zachradtka/5f73554a7ac7058f6b7b to your computer and use it in GitHub Desktop.
Save zachradtka/5f73554a7ac7058f6b7b to your computer and use it in GitHub Desktop.
Get location info for ip addresses
import sys
import re
import operator
import urllib.request
import json
def geolookup(ip, format):
# Create URL based on the IP address
geo_url = 'http://getcitydetails.geobytes.com/GetCityDetails?callback=?&fqcn={0}'.format(ip)
# Read the response
with urllib.request.urlopen(geo_url) as f:
response = f.read().decode('utf-8')
# Strip the unwanted characters from the beginning and ending of the response
json_dict = json.loads(response.lstrip('?(').rstrip(');').replace('geobytes',''))
# Ensure the retrieved IP address is the same as the submitted IP address
if json_dict['ipaddress'] != ip:
raise RuntimeError('GeoBytes error: Results don\'t match request -- got results for {0}'.format(json_dict['ipaddress']))
return {k: json_dict.get(k,None) for k in format}
def main():
argv = sys.argv
# Ensure that a file was specified on the command line
if len(argv) != 3:
print('Usage: {0} <inputFile> <outputFile>'.format(argv[0]), file=sys.stderr)
sys.exit()
numUsers = 0 # The total number of users
numInvalidIps = 0 # The total number of invalid IP addresses
numUnknownCountries = 0 # Total number of unknown countries
# Initialize variables
inputFile = argv[1]
outputFile = argv[2]
format = ['country', 'internet', 'region', 'code', 'city', 'latitude', 'longitude', 'fqcn', 'certainty']
# Read the input file
print('Reading from file: {0}'.format(inputFile))
# Read the file and store
with open(inputFile) as ifp, open(outputFile, 'w') as ofp:
for line in ifp:
# Remove the quotes and newline character from the line
noQuotes = re.sub('"', '', line).rstrip('\n')
# Convert the line to a list
user = noQuotes.split('\t')
# Grab the users IP address
ipAddr = user[1]
# Make sure the ipAddress is a valid string
if ipAddr:
try:
# Get the geo information
geoIp = geolookup(ipAddr, format)
# Write the results to a file
json.dump(geoIp, ofp, sort_keys=True)
ofp.write('\n')
except Exception as err:
print('Failed to look up {0}: {1}'.format(ipAddr, err))
else:
print('Error: uid:{0} with IP "{1}" is not valid'.format(user[0], ipAddr))
numInvalidIps += 1
# Increment the total number of users
numUsers += 1
# Display statistics
print('Total Number of Users: {0}'.format(numUsers))
print('Total Number of Invalid IPs: {0}'.format(numInvalidIps))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment