Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help me figure this out in python using API\'s? For this Now You Cod

ID: 3814758 • Letter: C

Question

Can someone help me figure this out in python using API's?

For this Now You Code, you will complete a very common task in data analytics: converting an IP address to an approximate location. Write a program to read the IP Addresses from the File NYC2-lP-Addresses.txt and for each IP address determine the approximate location (City and State) for the origin of that IP Address. This is usually done as part of analytics to determine the origins of website visitors. To perform the lookups, use the http://freegeoip net API. You'll have to read through the API documentation first and understand how to use the API before you write the entire program. As a first step I highly recommed that you get just the IP lookup working, perhaps writing it as a function, and then try to read from the file and perform the lookups for each IP address in the file. Here's a sample of a geoip lookup of the IP Address '128.230.182.217' {'city': 'Syracuse', 'country_code': 'US', 'country_name': 'United States', 'ip': '128.230.182.217', 'latitude': 43.0377, 'longitude': -76.1396, 'metro_code': 555, 'region_code': 'NY', 'region_name': 'New York', 'time_zone': 'America/New_York', 'zip_code': '13244'} In this example the city and state would be Syracuse, NY Final Program Output: IP: 128.122.140.238 LOCATION: New York, NY IP: 23.112.232.160 LOCATION: Green Bay, WI IP: 23.192.215.44 LOCATION: Cambridge, MA IP: 23.224.160.4 LOCATION: Cheyenne, WY IP: 23.230.12.5 LOCATION: San Dose, CA IP: 23.80.125.101 LOCATION: Phoenix, A2 IP: 23.83.132.200 LOCATION: Phoenix, AZ IP: 23.88.15.5 LOCATION: LOS Angeles, CA IP: 24.0.14.56 LOCATION: Iselin, ND IP: 24.1.25.140 LOCATION: Chicago, IL IP: 24.11.125.10 LOCATION: Orem, UT IP: 24.38.114.105 LOCATION: Matawan, ND IP: 24.38.224.161 LOCATION: Darien, CT IP: 56.216.127.219 LOCATION: Raleigh, NC IP: 68.199.40.156 LOCATION: Elmont, NY IP: 74.111.18.59 LOCATION: Auburn, NY IP: 74.111.6.173 LOCATION: Liverpool, NY IP: 98.29.25.44 LOCATION: Dayton, OH

Explanation / Answer

#!/usr/bin/env python
from urllib2 import urlopen
from contextlib import closing
import json
f = open('ip.txt', 'r')
lines = f.read().splitlines()
#print lines[0]
# creating a url and fetching the json data
for current in range(len(lines)):
url = 'http://freegeoip.net/json/'
url = url + lines[current]
try:
with closing(urlopen(url)) as response:
location = json.loads(response.read())
# print(location)
location_city = location['city']
location_regioncode = location['region_code']
print "IP:",
print lines[current],
print "LOCATION:",
print location_city,
print location_regioncode
except:
print("Location could not be determined")

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote