https://pypi.python.org/pypi/GeoIP/python
Requirementsmysql
Python 2.5+ or 3.3+git
GeoIP C Library 1.4.7 or greatergithub
Installationredis
With pip:sql
$ pip install GeoIP
bootstrap
From source:api
$ python setup.py build$ python setup.py install
python2.7
下載和安裝Pythonide
有個一個很是重要的步驟是咱們使用的是make altinstall。若是使用make install,你將會看到在系統中有兩個不一樣版本的Python在/usr/bin/目錄中。這將會致使不少問題,並且很差處理。
view sourceprint?
wget http://mirrors.sohu.com/python/2.7.9/Python-2.7.9.tgz
tar jxvf Python-2.7.5.tar.bz2
cd Python-2.7.5
./configure --prefix=/usr/local/python-2.7.9 --with-threads --enable-shared && make && make altinstall
#Python安裝軟件使用pipy工具
wget https://bootstrap.pypa.io/get-pip.py
/usr/local/python-2.7.9/bin/python2.7 get-pip.py
/usr/local/python-2.7.9/bin/pip install GeoIP
/usr/local/python-2.7.9/bin/pip install GeoIP2
/usr/local/python-2.7.9/bin/pip install MySQL-python
/usr/local/python-2.7.9/bin/pip install redis
Python 安裝 MaxMind GeoLite City
一、先安裝 geoip c library
geoip c library >= 1.4.6 installed on your machine. >= 1.4.6 installed on your machine.
wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.7.tar.gz
tar -xvzf GeoIP-1.4.7.tar.gz
cd GeoIP-1.4.7
./confiure
make
make install
二、下載GeoLite City Phthon版本
https://codeload.github.com/maxmind/geoip-api-python/zip/master
unzip master
$ python setup.py build
$ python setup.py install
三、下載GeoLiteCity.dat
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
四、用法
>>> import GeoIP
>>> geodb='/home/qinjianwang/maxMind/GeoLiteCity.dat'
>>> gi = GeoIP.open(geodb, GeoIP.GEOIP_STANDARD)
>>> ip='180.76.1.3'
>>> gi.record_by_addr(ip)
{'city': 'Beijing', 'region_name': 'Beijing', 'region': '22', 'area_code': 0, 'time_zone': 'Asia/Harbin', 'longitude': 116.38829803466797, 'metro_code': 0, 'country_code3': 'CHN', 'latitude': 39.92890167236328, 'postal_code': None, 'dma_code': 0, 'country_code': 'CN', 'country_name': 'China'}
>>> gir = gi.record_by_name("www.google.com")
>>> gir
{'city': 'Mountain View', 'region_name': 'California', 'region': 'CA', 'area_code': 650, 'time_zone': 'America/Los_Angeles', 'longitude': -122.05740356445312, 'metro_code': 807, 'country_code3': 'USA', 'latitude': 37.4192008972168, 'postal_code': '94043', 'dma_code': 807, 'country_code': 'US', 'country_name': 'United States'}
>>>
if gir is not None:
print(gir['country_code'])
print(gir['country_code3'])
print(gir['country_name'])
print(gir['city'])
print(gir['region'])
print(gir['region_name'])
print(gir['postal_code'])
print(gir['latitude'])
print(gir['longitude'])
print(gir['area_code'])
print(gir['time_zone'])
print(gir['metro_code'])
print(str(gir))
>>> gir['country_code'],gir['city'],gir['region_name']
('US', 'Mountain View', 'California')
>>>
注意:安裝完成後,最好執行一次 /sbin/ldconfig,不然在import GeoIP時,可能會報:
ibGeoIP.so.1: cannot open shared object file: No such file or directory
默認libGeoIP.so.1的路徑是 /usr/local/lib/libGeoIP.so.1,須要檢查/etc/ld.so.conf文件中是否有包含:
/usr/local/lib
若沒有,則加上此目錄再/sbin/ldconfig。
安裝MySQL-python
下載 MySQL for Python
地址:http://sourceforge.net/projects/mysql-python/files/mysql-python/
wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz
三、安裝
複製代碼 代碼以下:
$ cd MySQL-python-1.2.3
$ python setup.py build
$ python setup.py install
注:
若是在執行:python setup.py build 遇到如下錯誤:
複製代碼 代碼以下:
EnvironmentError: mysql_config not found
首先查找mysql_config的位置,使用
find / -name mysql_config ,好比個人在/usr/local/mysql/bin/mysql_config
修改setup_posix.py文件,在26行:
mysql_config.path = 「mysql_config」 修改成:
複製代碼 代碼以下:
mysql_config.path = 「/usr/local/mysql/bin/mysql_config」
保存後,而後再次執行:
複製代碼 代碼以下:
python setup.py build
python setup.py install
報錯2:找不到libperconaserverclient_r
ln -s /usr/local/mysql/lib/libperconaserverclient_r.so /usr/local/mysql/lib/mysql/
其它包在安裝pip後可直接安裝: