此API提供对Sypex Geo 2.2数据库的访问。数据库可从https://sypexgeo.net获得/
pysxgeo的Python项目详细描述
说明
pysxgeo是一个api,提供对sypex geo(2.2)数据库(国家和城市)的访问。 数据库按IP地址提供有关地理位置的信息。 有关数据库的信息,请访问sypexgeo.net。
安装
$ pip install pysxgeo
用法
一些示例:
>>> frompysxgeoimportsxgeo>>> >>> ip='77.236.97.247'>>> db='/path/to/db'# City or Country>>> # AVAILABLE MODES: SXGEO_FILE, SXGEO_MEMORY, SXGEO_BATCH>>> info=sxgeo.SxGeo(db_file=db,mode=sxgeo.SXGEO_BATCH)>>> info.get_city(ip,full=True){'country': {'id': 56, 'lon': 10.5, 'lat': 51.5, 'name_en': 'Germany', 'iso': 'DE', 'name_ru': 'Германия'}, 'region': {'id': 2861876, 'iso': 'DE-NW', 'name_en': 'Land Nordrhein-Westfalen', 'name_ru': 'Северный Рейн-Вестфалия'}, 'city': {'id': 2831105, 'lon': 6.36667, 'name_ru': 'Sonsbeck', 'name_en': 'Sonsbeck', 'lat': 51.61667}} >>> info.get_country(ip)'DE' >>> info.get_country_id(ip)56 >>> # Quick usage. The answer depends of your database choice.>>> info.get(ip){'city': {'name_en': 'Sonsbeck', 'lon': 6.36667, 'name_ru': 'Sonsbeck', 'id': 2831105, 'lat': 51.61667}, 'country': {'iso': 'DE', 'id': 56}}, >>> # Get coordinates tuple(latitude, longitude)>>> info.get_coords()# For the last ip(51.61667, 6.36667) >>> info.get_coords(ip)# For the new ip(51.61667, 6.36667) >>> # Get db info:>>> info.about()
注释
使用字符集“utf-8”处理数据库比使用字符集“cp1251”处理数据库快。