How to find location with IP address in Python?
How to find location with IP address in Python?
One of the simplest methods for getting the IP address as well as the location in detail is to use http://ipinfo.io
import re
import json
from urllib2 import urlopen
url = http://ipinfo.io/json
response = urlopen(url)
data = json.load(response)
IP=data[ip]
org=data[org]
city = data[city]
country=data[country]
region=data[region]
print Your IP detailn
print IP : {4} nRegion : {1} nCountry : {2} nCity : {3} nOrg : {0}.format(org,region,country,city,IP)
Try with pygeoip
~$ ping stackoverflow.com
PING stackoverflow.com (198.252.206.16) 56(84) bytes of data.
>>> import pygeoip
>>> GEOIP = pygeoip.GeoIP(/absolute_path/GeoIP.dat, pygeoip.MEMORY_CACHE)
>>> GEOIP.country_name_by_addr(ip)
United States
GeoIP.data is available here
How to find location with IP address in Python?
for python-3.x
def ipInfo(addr=):
from urllib.request import urlopen
from json import load
if addr == :
url = https://ipinfo.io/json
else:
url = https://ipinfo.io/ + addr + /json
res = urlopen(url)
#response from url(if res==None then check connection)
data = load(res)
#will load the json response into data
for attr in data.keys():
#will print the data line by line
print(attr, *13+t->t,data[attr])