IP歸屬地查詢

2021-06-12 21:01:02 字數 3569 閱讀 7297

#urllib:http api請求

##re:ip格式驗證、返回結果提取

tkinter:介面布局、樣式

##  ip定位,根據盛大ip查詢api所編寫

## anthor:yanzhenwei([email protected])

## create version: v1.0beta01 2012-03-02

## last update:2012-03-03

from tkinter import *

import re

import urllib

import urllib.request

class mwindow(frame):

def __init__(self):

frame.__init__(self)

self.master.title('ipsearch')

self.master.geometry('650x400')

self.pack(side = top,expand = yes,fill = both)

ipvar = stringvar()

res = {}

res['result'] = stringvar()

res['country'] = stringvar()

res['city'] = stringvar()

res['sp'] = stringvar()

# top frame

topf = self.createframe(self,top)

ipe = self.createentry(topf,top,ipvar)

resl = self.createlabel(topf,top,'查詢結果:')

rese = self.createentry(topf,top,res['result'])

#mid frame

midf = self.createframe(self,top)

conl = self.createlabel(midf,left,'省')

cone = self.createentry(midf,left,res['country'])

citl = self.createlabel(midf,left,'市')

cite = self.createentry(midf,left,res['city'])

conl = self.createlabel(midf,left,'運營商')

spe = self.createentry(midf,left,res['sp'])

#bottom frame

botf = self.createframe(self,top)

searchb = button(botf,text = 'search',

command = lambda ipvar = ipvar,res = res:self.ipsearch(ipvar,res))

searchb.pack(side = top,expand = yes,fill = both)

def createframe(self,parent,side):

f = frame(parent)

f.pack(side = side,expand = yes,fill = both)

return f

def createlabel(self,parent,side,text):

l = label(parent,text = text)

l.pack(side = side,expand = yes)

return l

def createentry(self,parent,side,textvariable):

e = entry(parent,relief = sunken,textvariable = textvariable,width = 20)

e.pack(side = side,expand = no)

return e

def ipsearch(self,ipvar,res):

iprex = '((?:(?:25[0-5]|2[0-4]\d|((1\d)|([1-9]?\d)))\.)(?:25[0-5]|2[0-4]\d|((1\d)|([1-9]?\d))))'

ip = ipvar.get()

tmp = re.findall(re.compile(iprex),ip)

if not tmp:

res['result'].set('ip格式錯誤,請重新輸入')

return

data = ips().search(ip)

if data['result'] == '1':

data['result'] = '該ip所在位置如下'

else:

data['result'] = '找不到相關資訊'

res['result'].set(data['result'])

res['country'].set(data['country'])

res['city'].set(data['city'])

res['sp'].set(data['sp'])

class ips():

def __init__(self):

self.apiurl = ''

def search(self,ip):

headers = ('user-agent','mozilla/5.0 (windows nt 5.1; rv:14.0) gecko/20100101 firefox/14.0.1')

opener = urllib.request.build_opener()

opener.addheaders = [headers]

data = opener.open(self.apiurl+ip).read()

data = data.decode('utf-8')

datalist = {}

patterns = '([0-9])'

datalist['result'] = re.findall(re.compile(patterns),data)[0]

patterns = '([^x00-xff]+)'

datalist['country'] = re.findall(re.compile(patterns),data)[0]

patterns = '([^x00-xff]+)'

datalist['city'] = re.findall(re.compile(patterns),data)[0]

patterns = '([^x00-xff]+)'

datalist['sp'] = re.findall(re.compile(patterns),data)[0]

return datalist

if __name__ == '__main__':

mwindow().mainloop()

python查詢ip歸屬地

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

查詢ip歸屬地 小應用

快過年了,寫個小玩意 下面這個應用涉及到了一些基礎東西.1 http api呼叫 2 asynctask使用 3 textview 自動匹配 4 string 的spilt的正規表示式 應用截圖 截圖傳不上來.貼下邊 public class mainactivity extends activit...

如何快速查詢IP歸屬地

這兩天遇到這麼乙個問題,就是查詢乙個ip的歸屬地。當然我會有乙個ip段的分配列表,格式如下 16777472 16778239 xx省 xx市 第一列是ip段的起始ip,第二列是ip段的終止ip,第三列是對應的省份。一共大概200萬條左右。那麼好了,如何進行查詢呢?想到的最簡單的辦法,就是二分查詢。...