python 處理IP所屬地

2021-08-24 17:51:44 字數 1766 閱讀 3351

先安裝這兩個

pip install python-geoip-geolite2 -i

pip install geoip2

搜一搜geolite2-country.mmdb第乙個鏈結就有

測試**

import geoip2.database

#geolite2-city.mmdb檔案的位置

reader = geoip2.database.reader('./geolite2-city.mmdb')

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庫解析出城市位置資訊

#!/usr/bin/env python

#encoding:utf8

#author: zeping lai

import geoip2.database

reader = geoip2.database.reader('e:/tools/geolite2-city.mmdb')

response = reader.city("202.96.128.166")

print "\n--- 國家 --------"

print response.country.iso_code

print response.country.name

print response.country.names['zh-cn']

print "\n--- 省份 --------"

print response.subdivisions.most_specific.iso_code

print response.subdivisions.most_specific.name

print response.subdivisions.most_specific.names['zh-cn']

print "\n--- 城市 --------"

print response.postal.code

print response.city.name

print response.city.names['zh-cn']

print "\n--- 位置(經緯度) --------"

print response.location.latitude

print response.location.longitude

查詢IP所屬地

做了查詢ip所屬地的程式,將過程記錄如下 1,建立資料庫 得到的是乙個.dat檔案,通過iplook工具轉換成了.txt格式的,然後匯入了access中,最後匯入到ms sqlserver中.ip資料庫有三個字段,startip decimal 18,0 endip decimal 18,0 add...

Python批量判斷IP位址所屬地區

首先安裝python擴充套件庫netaddr,然後對下面的 進行簡單修改後即可滿足某些場合的應用。from random import randrange from netaddr import iprange defiprangetest ipaddr,iprange 遍歷ip位址與地區分布對應關...

python查詢ip歸屬地

本來想呼叫阿里的ip介面查詢ip歸屬地。結果發現阿里的介面非常不給力,主要是不準確,不過是免費的且有地區和isp的資訊。以下是實現 coding utf 8 import requests def checkip ip url try r requests.get url,params ip,tim...