python 查询IP地址所在城市

#encoding=utf-8

import time
import geoip2.database

reader = geoip2.database.Reader('GeoLite2-City.mmdb')

response = reader.city("219.155.85.11")
_city = response.city.names.get('zh-CN','')
print(_city)

_province = response.subdivisions.most_specific.names['zh-CN']
print(_province)

_county = response.country.names['zh-CN']
print(_county)


>>> #encoding=utf-8
...
>>> import time
>>> import geoip2.database

>>> reader = geoip2.database.Reader('GeoLite2-City.mmdb')
>>> response = reader.city("219.155.85.11")
>>> response
geoip2.models.City({'city': {'geoname_id': 1784658, 'names': {'de': 'Zhengzhou', 'en': 'Zhengzhou', 'es': 'Zhengzhou', 'fr': 'Zhengzhou', 'ja': '鄭州市', 'pt-BR': 'Zhengzhou', 'ru': 'Чжэнчжоу', 'zh-CN': '郑州'}}, 'continent': {'code': 'AS', 'geoname_id': 6255147, 'names': {'de': 'Asien', 'en': 'Asia', 'es': 'Asia', 'fr': 'Asie', 'ja': 'アジア', 'pt-BR': 'Ásia', 'ru': 'Азия', 'zh-CN': '亚洲'}}, 'country': {'geoname_id': 1814991, 'iso_code': 'CN', 'names': {'de': 'China', 'en': 'China', 'es': 'China', 'fr': 'Chine', 'ja': '中国', 'pt-BR': 'China', 'ru': 'Китай', 'zh-CN': '中国'}}, 'location': {'accuracy_radius': 1, 'latitude': 34.6836, 'longitude': 113.5325, 'time_zone': 'Asia/Shanghai'}, 'registered_country': {'geoname_id': 1814991, 'iso_code': 'CN', 'names': {'de': 'China', 'en': 'China', 'es': 'China', 'fr': 'Chine', 'ja': '中国', 'pt-BR': 'China', 'ru': 'Китай', 'zh-CN': '中国'}}, 'subdivisions': [{'geoname_id': 1808520, 'iso_code': 'HA', 'names': {'en': 'Henan', 'fr': 'Province de Henan', 'zh-CN': '河南'}}], 'traits': {'ip_address': '219.155.85.11'}}, ['en'])

>>> _city = response.city.names.get('zh-CN','')
>>> _city
'郑州'

_city = response.country.get('zh-CN','')

#测试
python ip.py
郑州
河南
中国
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注