从python代码访问sypex geo-ip数据库文件数据的api
pysyge的Python项目详细描述
http://github.com/idlesign/pysyge
那是什么
pysyge是从python代码中访问sypex geo-ip数据库文件数据的api。
有关Sypex地理数据库及其功能的更多信息,请访问http://sypexgeo.net。
随着时间的推移,可能会断开的直接数据库链接:
- Sypex Geo City DB - http://sypexgeo.net/files/SxGeoCity_utf8.zip
要求
- Python2.7+,3.5+
注意
- 此版本的pysyge适用于sypex geo db版本2.2或更高版本。
The structure of a dictionary returned by GeoLocator.get_location() was preserved in a backward compatible manner as much as possible, yet it’s advised to update your code to use data from info sub dictionary.
- 此版本的pysyge与utf-8sypex geo数据库一起工作。pysyge返回的文本是utf-8。
快速启动
从http://sypexgeo.net/下载geo-ip数据库文件(下面的示例使用SxGeoCityMax.datfile)。
应用程序示例
# Import all we need from pysyge module.frompysygeimportGeoLocator,MODE_BATCH,MODE_MEMORY# Create GeoLocator object to access API# from 'SxGeoCityMax.dat' using fast memory mode.geodata=GeoLocator('SxGeoCityMax.dat',MODE_BATCH|MODE_MEMORY)# Let's get some meta information.print('DB version %s (%s)'%(geodata.get_db_version(),geodata.get_db_date()))# Request geo information for 77.88.21.3 IP address.# Getting detailed information, including region info.location=geodata.get_location('77.88.21.3',detailed=True)# Print out some lyrics.# Most interesting data is under `info` in `city`, `country` and `region` dictionariesprint('%s (%s) calling. All the circuits are busy.'%(location['info']['city']['name_en'],location['info']['country']['iso']))