Python2018/labs04/examples/25_ip2geolocation.py

32 lines
897 B
Python
Raw Permalink Normal View History

2018-05-12 13:16:05 +02:00
import csv
import requests
def get_address():
"""
Given a CSV file, this function returns a list of lists
where each element (list) in the outer list contains the
row info from the csv file.
"""
all_addresses = [requests.get('http://ip.42.pl/raw').text.strip()]
return all_addresses
def get_geolocation(ip_address):
"""
Given a list of lists from `get_addresses()`, this function
returns an updated lists of lists containing the geolocation.
"""
print("Getting geo information...")
# update header
# get geolocation
for line in all_the_ip_address:
print("Grabbing geo info")
r = requests.get('https://freegeoip.net/json/{0}'.format(line))
print([str(r.json()['country_name']), str(r.json()['city'])])
if __name__ == '__main__':
all_the_ip_address = get_address()
get_geolocation(all_the_ip_address)