python使用GeoIP的本地化
GeoIP用来获取IP地址的详细信息
找了几个python关于GeoIP本地化的库,如pygeoip、GeoIP等,最终选用geoip2 https://pypi.python.org/pypi/geoip2
sudo pip install geoip2
安装有点坑,网速的问题,于是下载了源码进行安装。
sudo wget https://pypi.python.org/packages/source/g/geoip2/geoip2-2.2.0.tar.gz#md5=26259d212447bc840400c25a48275fbc
sudo python setup.py install
安装时发现依赖maxminddb,并且自动去search google.com的下载源了,只好手动安装
sudo pip install maxminddb
又提示依赖ipaddr,又跑去搜google,
sudo pip install ipaddr
如果pip无下载速度,可以网上找下载源码安装
三者装完之后,终于可以用了
根据文档,本地化的使用方式:
>>> import geoip2.database
>>>
>>> # This creates a Reader object. You should use the same object
>>> # across multiple requests as creation of it is expensive.
>>> reader = geoip2.database.Reader('/path/to/GeoLite2-City.mmdb')
>>>
>>> # Replace "city" with the method corresponding to the database
>>> # that you are using, e.g., "country".
>>> response = reader.city('128.101.101.101')
>>>
>>> response.country.iso_code
'US'
>>> response.country.name
'United States'
>>> response.country.names['zh-CN']
u'美国'
>>>
>>> response.subdivisions.most_specific.name
'Minnesota'
>>> response.subdivisions.most_specific.iso_code
'MN'
>>>
>>> response.city.name
'Minneapolis'
>>>
>>> response.postal.code
'55455'
>>>
>>> response.location.latitude
44.9733
>>> response.location.longitude
-93.2323
>>> reader.close()
找了几个python关于GeoIP本地化的库,如pygeoip、GeoIP等,最终选用geoip2 https://pypi.python.org/pypi/geoip2
sudo pip install geoip2
安装有点坑,网速的问题,于是下载了源码进行安装。
sudo wget https://pypi.python.org/packages/source/g/geoip2/geoip2-2.2.0.tar.gz#md5=26259d212447bc840400c25a48275fbc
sudo python setup.py install
安装时发现依赖maxminddb,并且自动去search google.com的下载源了,只好手动安装
sudo pip install maxminddb
又提示依赖ipaddr,又跑去搜google,
sudo pip install ipaddr
如果pip无下载速度,可以网上找下载源码安装
三者装完之后,终于可以用了
根据文档,本地化的使用方式:
>>> import geoip2.database
>>>
>>> # This creates a Reader object. You should use the same object
>>> # across multiple requests as creation of it is expensive.
>>> reader = geoip2.database.Reader('/path/to/GeoLite2-City.mmdb')
>>>
>>> # Replace "city" with the method corresponding to the database
>>> # that you are using, e.g., "country".
>>> response = reader.city('128.101.101.101')
>>>
>>> response.country.iso_code
'US'
>>> response.country.name
'United States'
>>> response.country.names['zh-CN']
u'美国'
>>>
>>> response.subdivisions.most_specific.name
'Minnesota'
>>> response.subdivisions.most_specific.iso_code
'MN'
>>>
>>> response.city.name
'Minneapolis'
>>>
>>> response.postal.code
'55455'
>>>
>>> response.location.latitude
44.9733
>>> response.location.longitude
-93.2323
>>> reader.close()