ip2location for finding ip address of a specific location only
Question:
I am using ip2location to find out location of a list of ip address in a file called output.txt and write the answer in another file ip_info.txt.I want to write only those entries in my file whose ip address is of US.The following is my code for doing this.
import IP2Location;
import urllib;
import time;
start_time = time.time()
IP2LocObj = IP2Location.IP2Location();
IP2LocObj.open("data/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN");
t=open('ip_info','w');
with open('output','r') as f: # file containing ip addresses
for line_terminated in f:
line= line_terminated.rstrip('rn'); # strip newline
if line: # non-blank lines
rec = IP2LocObj.get_all(line);
if(rec.country_short == 'US')
t.write(line);
t.write('t');
t.write(rec.country_short);
t.write('n');
print("--- %s seconds ---" % (time.time() - start_time))
I am getting the following error here.
File "myprogram.py", line 13
if(rec.country_short == 'US')
SyntaxError: invalid syntax
For reference you can check out https://www.ip2location.com/developers/python
Answers:
if(rec.country_short == 'US')
is not valid Python.
Did you mean:
if rec.country_short == 'US':
?
all code
import IP2Location;
import urllib;
import time;
start_time = time.time()
IP2LocObj = IP2Location.IP2Location();
IP2LocObj.open("data/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN");
t=open('ip_info','w');
with open('output','r') as f:
for line_terminated in f:
line= line_terminated.rstrip('rn')
if line:
rec = IP2LocObj.get_all(line)
if rec.country_short == 'US':
t.write(line);
t.write('t');
t.write(rec.country_short);
t.write('n');
print("--- %s seconds ---" % (time.time() - start_time))
did you mean like this
find out location of a list of ip address in a file called output.txt and write the answer in another file ip_info.txt
For IP Geolocation batch or bulk lookup, you could use IPinfo.io‘s batch lookup feature. You can use the IPinfo Python Module and read the documentation on the batch operations here. You also don’t need to deal with a database. You are simply using the IPinfo API, so get the token from you dashboard after you have signed up.
import ipinfo
import time
start_time = time.time()
# initialize handler with access token
# get your access token from your account dashboard
access_token = "insert_your_token_here"
handler = ipinfo.getHandler(access_token)
def write_ipinfo(data):
'''function to write IP geolocation information'''
with open('ip_info', 'a') as file: # 'a' stands for append
file.write(f"{data}n") # adds a new line at the end of each entry
with open("output") as ip_file:
'''this file contains the ip_address data'''
ip_addresses = ip_file.read().split("n") # converted to a list
# batch IP geolocation lookup using IPinfo API
ip_geolocation_info = handler.getBatchDetails([ip_addresses])
for key, value in ip_geolocation_info.items():
if value["country"] == "US": # this is where the bug was
data = f'{value["ip"]}t{value["country"]}' # using f-string to format
write_ipinfo(data) # writing your data to the file
print("--- %s seconds ---" % (time.time() - start_time))
Example of individual IP geolocation lookup response. The IP address is the key, and the value is the geolocation information response.
{'8.8.8.8': {'city': 'Mountain View',
'country': 'US',
'country_name': 'United States',
'hostname': 'dns.google',
'ip': '8.8.8.8',
'latitude': '37.3860',
'loc': '37.3860,-122.0838',
'longitude': '-122.0838',
'org': 'AS15169 Google LLC',
'postal': '94035',
'region': 'California',
'timezone': 'America/Los_Angeles'}}
I am using ip2location to find out location of a list of ip address in a file called output.txt and write the answer in another file ip_info.txt.I want to write only those entries in my file whose ip address is of US.The following is my code for doing this.
import IP2Location;
import urllib;
import time;
start_time = time.time()
IP2LocObj = IP2Location.IP2Location();
IP2LocObj.open("data/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN");
t=open('ip_info','w');
with open('output','r') as f: # file containing ip addresses
for line_terminated in f:
line= line_terminated.rstrip('rn'); # strip newline
if line: # non-blank lines
rec = IP2LocObj.get_all(line);
if(rec.country_short == 'US')
t.write(line);
t.write('t');
t.write(rec.country_short);
t.write('n');
print("--- %s seconds ---" % (time.time() - start_time))
I am getting the following error here.
File "myprogram.py", line 13
if(rec.country_short == 'US')
SyntaxError: invalid syntax
For reference you can check out https://www.ip2location.com/developers/python
if(rec.country_short == 'US')
is not valid Python.
Did you mean:
if rec.country_short == 'US':
?
all code
import IP2Location;
import urllib;
import time;
start_time = time.time()
IP2LocObj = IP2Location.IP2Location();
IP2LocObj.open("data/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN");
t=open('ip_info','w');
with open('output','r') as f:
for line_terminated in f:
line= line_terminated.rstrip('rn')
if line:
rec = IP2LocObj.get_all(line)
if rec.country_short == 'US':
t.write(line);
t.write('t');
t.write(rec.country_short);
t.write('n');
print("--- %s seconds ---" % (time.time() - start_time))
did you mean like this
find out location of a list of ip address in a file called output.txt and write the answer in another file ip_info.txt
For IP Geolocation batch or bulk lookup, you could use IPinfo.io‘s batch lookup feature. You can use the IPinfo Python Module and read the documentation on the batch operations here. You also don’t need to deal with a database. You are simply using the IPinfo API, so get the token from you dashboard after you have signed up.
import ipinfo
import time
start_time = time.time()
# initialize handler with access token
# get your access token from your account dashboard
access_token = "insert_your_token_here"
handler = ipinfo.getHandler(access_token)
def write_ipinfo(data):
'''function to write IP geolocation information'''
with open('ip_info', 'a') as file: # 'a' stands for append
file.write(f"{data}n") # adds a new line at the end of each entry
with open("output") as ip_file:
'''this file contains the ip_address data'''
ip_addresses = ip_file.read().split("n") # converted to a list
# batch IP geolocation lookup using IPinfo API
ip_geolocation_info = handler.getBatchDetails([ip_addresses])
for key, value in ip_geolocation_info.items():
if value["country"] == "US": # this is where the bug was
data = f'{value["ip"]}t{value["country"]}' # using f-string to format
write_ipinfo(data) # writing your data to the file
print("--- %s seconds ---" % (time.time() - start_time))
Example of individual IP geolocation lookup response. The IP address is the key, and the value is the geolocation information response.
{'8.8.8.8': {'city': 'Mountain View',
'country': 'US',
'country_name': 'United States',
'hostname': 'dns.google',
'ip': '8.8.8.8',
'latitude': '37.3860',
'loc': '37.3860,-122.0838',
'longitude': '-122.0838',
'org': 'AS15169 Google LLC',
'postal': '94035',
'region': 'California',
'timezone': 'America/Los_Angeles'}}