Как найти местоположение с IP-адресом в Python?
Я разрабатываю проект, который должен хранить местоположение пользователя в моей базе данных. Я получил публичный IP-адрес этого пользователя. Но я не могу получить местоположение пользователя. Я пробовал несколько способов (из StackOverflow), но я не нашел подсказки. Как ниже
url = urllib.urlopen("http://api.hostip.info/get_html.php?ip=%s&position=true" % ip)
data = re.compile('^[^\(]+\(|\)$').sub('', url.read())
print data
но я получаю результат как
Unknown Country?) (XX)
City: (Unknown City?)
по-другому:
import urllib
response = urllib.urlopen("http://api.hostip.info/get_html.php?ip={}&position=true".format(ip)).read()
print(response)
но результат
Country: (Unknown Country?) (XX)
City: (Unknown City?)
Latitude:
Longitude:
IP: 115.xxx.xxx.xx
Любая помощь будет оценена!
Ответы
Ответ 1
Один из простейших способов получения IP-адреса, а также подробное расположение - использовать 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 detail\n '
print 'IP : {4} \nRegion : {1} \nCountry : {2} \nCity : {3} \nOrg : {0}'.format(org,region,country,city,IP)
Ответ 2
Попробуйте 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 доступен здесь
Ответ 3
Предполагая, что вы уже получили IP-адрес, вы можете попытаться использовать библиотеку Python IP2Location для определения местоположения пользователя. Пример кода выглядит следующим образом:
import os
import IP2Location
database = IP2Location.IP2Location(os.path.join("data", "IPV4-COUNTRY.BIN"))
rec = database.get_all(ip)
print(rec.country_short)
print(rec.country_long)
print(rec.region)
print(rec.city)
print(rec.isp)
print(rec.latitude)
print(rec.longitude)
print(rec.domain)
print(rec.zipcode)
print(rec.timezone)
print(rec.netspeed)
print(rec.idd_code)
print(rec.area_code)
print(rec.weather_code)
print(rec.weather_name)
print(rec.mcc)
print(rec.mnc)
print(rec.mobile_brand)
print(rec.elevation)
print(rec.usage_type)
Зависит от вашего требования, например, если вы хотите получить имя страны и региона пользователя, вы можете сделать это:
import os
import IP2Location
database = IP2Location.IP2Location(os.path.join("data", "IPV4-COUNTRY.BIN"))
rec = database.get_all(ip)
user_country = rec.country_long
user_region = rec.region
Подробнее об этом можно узнать здесь: Библиотека Python IP2Location
Ссылка на Github: IP2Location Библиотека Python Github
Ответ 4
для 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])
Ответ 5
Я делаю то же самое на собственном сервере. Получите ключ API из http://ipinfodb.com/register.php и попробуйте:
import requests
ipdb = "http://api.ipinfodb.com/v3/ip-city/?key=<your api key>&ip="
ip_address = function_to_get_ip_address()
location = " ".join(str(requests.get(ipdb+ip_address).text).split(";")[4:7])
Значение location
будет COUNTRY REGION CITY
.
Имейте в виду, что IP-адреса не являются точными геолокаторами. Особенно, когда вы получаете доступ к своему сайту с мобильного устройства, вы увидите, что местоположение IP-адреса должно быть в 100 милях от физического местоположения пользователя.
Ответ 6
В конечном итоге это зависит от того, как вы получаете IP-адрес своих компьютеров. Если вы находитесь в VPN или другой частной сети, просто получение локального IP-адреса ничего не вернет, как вы видите сейчас. В этом случае вы должны получить общедоступный IP-адрес:
url = 'http://api.hostip.info/get_json.php'
info = json.loads(urllib.urlopen(url).read())
ip = info['ip']
Вот мой полный код для получения всей информации, которую вы ищете (я использовал freegeoip.net):
import urllib
import json
url = 'http://api.hostip.info/get_json.php'
info = json.loads(urllib.urlopen(url).read())
ip = info['ip']
urlFoLaction = "http://www.freegeoip.net/json/{0}".format(ip)
locationInfo = json.loads(urllib.urlopen(urlFoLaction).read())
print 'Country: ' + locationInfo['country_name']
print 'City: ' + locationInfo['city']
print ''
print 'Latitude: ' + str(locationInfo['latitude'])
print 'Longitude: ' + str(locationInfo['longitude'])
print 'IP: ' + str(locationInfo['ip'])
Ответ 7
требования:
sudo add-apt-repository ppa:maxmind/ppa
sudo apt update
sudo apt install libmaxminddb0 libmaxminddb-dev mmdb-bin
sudo pip install geoip2
база данных geoip:
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
tar xvfz GeoLite2-City.tar.gz
пример для журналов доступа nginx:
python -c 'import geoip2.database
reader = geoip2.database.Reader("./GeoLite2-City/GeoLite2-City.mmdb")
for line in open("/var/log/nginx/access.log').readlines():
response = reader.city(line.split(" ")[0])
print(dir(response))
'
связанные с:
Ответ 8
Вы можете использовать услуги https://geoip-db.com IPv4 и IPv6 поддерживаются. Возвращается функция обратного вызова JSON-объекта или JSONP.
Python 2:
import urllib
import json
url = "https://geoip-db.com/json"
response = urllib.urlopen(url)
data = json.loads(response.read())
print data
Python 3:
import urllib.request
import json
with urllib.request.urlopen("https://geoip-db.com/json") as url:
data = json.loads(url.read().decode())
print(data)
Пример Python 3 jsonp:
import urllib.request
import json
with urllib.request.urlopen("https://geoip-db.com/jsonp/8.8.8.8") as url:
data = url.read().decode()
data = data.split("(")[1].strip(")")
print(data)
Ответ 9
Я обнаружил, что ipinfo предлагает лучший сервис и предоставляет бесплатное использование API для вызовов до 50 тыс. В месяц - см. "Ограничения скорости" здесь:
import ipinfo
access_token = '123456789abc'
handler = ipinfo.getHandler(access_token)
ip_address = '216.239.36.21'
details = handler.getDetails(ip_address)
details.city
'Mountain View'
details.country
'US'
details.loc
'37.3861,-122.0840'
Ответ 10
https://github.com/airakesh/BeautifulSoupRecipes/blob/master/geoip.py
# Get Geolocation(Country) and hostname by passing a file having a bunch of IP addresses as the argument from the command line. Example- python GeoIP.py path-to-file-containing-IP addresses:
https://github.com/airakesh/BeautifulSoupRecipes/blob/master/sample_ips.txt
import re
import os
import sys
import subprocess
import socket
# Input file argument
ips_file = sys.argv[1]
# The regular expression for validating an IP-address
pattern = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)'''
def getGeoHost():
fp = open(ips_file, 'rb')
for line in fp:
line = line.strip()
addr = line.decode('utf-8')
regex = re.compile(pattern)
match = regex.match(addr)
# Get hostname by IP address
try:
host = socket.gethostbyaddr(addr)
hostname = host[0]
# Print Unknown no hostname is available
except:
hostname = 'Unknown'
# Get geolocation by IP address
get_geo_cmd = 'geoiplookup ' + addr
geo_str = subprocess.check_output(get_geo_cmd, shell=True)
geo = geo_str.decode('utf-8')
# Match country name pattern
geo_pattern = '''^(GeoIP Country Edition: ([A-Z]{2})\, (.*))'''
geo_regex = re.compile(geo_pattern)
country_match = re.match(geo_pattern, geo)
# Check country name is available and if not, print 'Unknown'
if country_match != '' and geo_pattern:
try:
country = country_match.group(3)
except:
country = 'Unknown'
# Clubbing together in format 'IP|Country|Hostname' data
geo_hostname = addr + ' | ' + country + ' | ' + hostname
print geo_hostname
if __name__ == "__main__":
ips_detail_list = getGeoHost()