<jupyter_start><jupyter_text>Now You Code 2: IP AddressesFor this Now You Code, you will complete a very common task in data analytics: converting an IP address https://en.wikipedia.org/wiki/IP_address to an approximate location.Write a program to read the IP Addresses from the File `NYC2-IP-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 program. **Once again, problem simplification is key here.  Just get the  IP lookup working, 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 `'192.168.127.12'````{'city': 'Syracuse', 'country_code': 'US', 'country_name': 'United States', 'ip': '192.168.127.12', '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 will read all the addresses from the file.:```IP: 192.168.127.12 LOCATION: New York,NYIP: 172.16.58.3 LOCATION: Green Bay,WIIP: 172.16.58.3 LOCATION: Cambridge,MAIP: 172.16.31.10 LOCATION: Cheyenne,WYIP: 172.16.58.3 LOCATION: San Jose,CAIP: 172.16.58.3 LOCATION: Phoenix,AZIP: 192.168.3.11 LOCATION: Phoenix,AZIP: 172.16.17.32 LOCATION: Los Angeles,CAIP: 172.16.58.3 LOCATION: Iselin,NJIP: 172.16.31.10 LOCATION: Chicago,ILIP: 24.11.125.10 LOCATION: Orem,UTIP: 172.16.31.10 LOCATION: Matawan,NJIP: 172.16.31.10 LOCATION: Darien,CTIP: 172.16.31.10 LOCATION: Raleigh,NCIP: 172.16.17.32 LOCATION: Elmont,NYIP: 172.16.58.3 LOCATION: Auburn,NYIP: 172.16.17.32 LOCATION: Liverpool,NYIP: 172.16.31.10 LOCATION: Dayton,OH```  Step 1: Problem Analysis for `geoiplookup` functionInputs: IP addressOutputs: Dictionary of Geographic information for that IP AddressAlgorithm (Steps in Program):import requestdefine function geoiplookupset url to url and have parametergo to dictionary ad find the keys that then need to be printed<jupyter_code># Step 2: write the user defined function `geoiplookup`
import requests
import json
        
def geoiplookup(location):
    params = {'ip' : location}
    url= 'http://freegeoip.net/' + location
    response = requests.get(url, params = params)            
    geoip = response.json()
    where = geoip['city'] + ' , ' + geoip['region_code']
    return where<jupyter_output><empty_output><jupyter_text>Step 3: Problem Analysis for entire programInputs: Outputs: Algorithm (Steps in Program):<jupyter_code># Step 4: write main program here
def geoiplookup(location):
    params = {'ip' : location}
    url= 'http://freegeoip.net/' + location
    response = requests.get(url, params = params)            
    geoip = response.json()
    where = geoip['city'] + ' , ' + geoip['region_code']
    return where

lines = 0

try:
    filename = "NYC2-IP-Address.txt"
    with open(filename, 'r') as f:
        for line in f.readlines():
            lines = lines + 1
            print("IP: " + line.strip() + " " + "LOCATION:" + geoiplookup(line.strip()))
            
except json_decoder.JSONDecodeError as e:
    print("ERROR: Cannot decode the response into json")
    print("DETAILS", e)
    
except requests.exceptions.HTTPerror as e:
    print("ERROR: Response from", url, 'was not ok.')
    print("DETAILS", e)
    
except requests.exceptions.RequestException as e:
    print("ERROR: Cannot connect to ", url)
    print("DETAILS", e)
except IndexError:
    print("Error: cannot read file," filename)
        
url= http://freegeoip.net
    city region code, region name<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11664548919949175, 'fraction_numerical': 0.05311308767471411, 'mean_word_length': 4.118335500650195, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 20, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '472753', 'license': 'MIT', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1225, 'n_words': 435, 'path': 'content/lessons/11/Now-You-Code/NYC2-Lookup-IP-Addresses.ipynb', 'repo_name': 'cspelz-su/Final-Project', 'size': '3947'}