用python的tkinter模块写了个查询IP地址的小程序
2013-08-04 01:20
513 查看
网工会经常做一件事情,就是在排查网络故障的时候,通过traceroute目的IP的方式,然后打开百度或者谷歌,查其中某一跳的IP地址属于那个城市,ISP信息。
这个效率比较低,下面我通过淘宝IP地址库提供的API封装一个小工具,就可以实时查询IP地址了。有2个版本,一个是Linux 命令行版,另一个是GUI版本;
淘宝IP地址库地址:http://ip.taobao.com
接口地址:http://ip.taobao.com/instructions.php
使用的模块:
1、json
由于接口返回的数据是json格式,自然就需要解析json的模块;
2、urllib
这个用于发起http请求,并返回数据,值得注意的是python 2.X版本和3.X版本存在差异;
3、tkinter
这个是python自带的GUI模块,简介用起来挺方便的;
4、sys
Linux版本通过sys模块的argv获取命令行参数
5、ctypes
该模块是在window GUI版当中,为了隐藏console这个黑色大框框用的;
Linux 命令行版:
b)process_data——处理数据,取出需要的信息,如ISP、国家、省市等信息;
c)output_data——格式化输出数据;
程序几个函数的执行顺序是:
1、__init__
初始化界面,绑定按钮事件,然后进入循环等待用户输入;
2、点击确认后,执行process_input函数,该函数会继续调用process_data函数,返回数据后,显示到界面上;
查询61.135.157.156的IP地址归属:
代码实现
这个效率比较低,下面我通过淘宝IP地址库提供的API封装一个小工具,就可以实时查询IP地址了。有2个版本,一个是Linux 命令行版,另一个是GUI版本;
淘宝IP地址库地址:http://ip.taobao.com
接口地址:http://ip.taobao.com/instructions.php
使用的模块:
1、json
由于接口返回的数据是json格式,自然就需要解析json的模块;
2、urllib
这个用于发起http请求,并返回数据,值得注意的是python 2.X版本和3.X版本存在差异;
3、tkinter
这个是python自带的GUI模块,简介用起来挺方便的;
4、sys
Linux版本通过sys模块的argv获取命令行参数
5、ctypes
该模块是在window GUI版当中,为了隐藏console这个黑色大框框用的;
Linux 命令行版:
说明
1、python版本2.7.3
2、整个代码分三个函数:
a)get_data——用于发起http请求,返回json格式数据;b)process_data——处理数据,取出需要的信息,如ISP、国家、省市等信息;
c)output_data——格式化输出数据;
运行效果
./queryip.py 61.135.157.156 country area region city county isp 中国 华北 北京市 北京市 NULL 联通
代码实现
#!/usr/local/bin/python #-*- coding: utf-8 -*- import json import urllib import sys def get_data(ip): API = "http://ip.taobao.com/service/getIpInfo.php?ip=" url = API + sys.argv[1] jsondata = json.loads(urllib.urlopen(url).read()) if jsondata['code'] == 1: print("No %s info." % ip) exit(1) else: return jsondata def process_data(jsondata): if jsondata['data']['country']: country = jsondata['data']['country'] else: country = "NULL" if jsondata['data']['area']: area = jsondata['data']['area'] else: area = "NULL" if jsondata['data']['region']: region = jsondata['data']['region'] else: region = "NULL" if jsondata['data']['city']: city = jsondata['data']['city'] else: city = "NULL" if jsondata['data']['county']: county = jsondata['data']['county'] else: county = "NULL" if jsondata['data']['isp']: isp= jsondata['data']['isp'] else: isp = "NULL" return (country, area, region, city, county, isp) def output_data(data): ret = "%-4s\t%-4s\t%-4s\t%-4s\t%-4s\t%-4s\n" % ("country", "area", "region", "city", "county", "isp") ret += "%-4s\t%-4s\t%-4s\t%-4s\t%-4s\t%-4s" % data print(ret) #main ret = get_data(sys.argv[1]) result = process_data(ret) output_data(result)
window GUI版
和Linux的主要区别是用了类封装,添加了一个构造函数,在这里执行绘制界面的一系列的初始化动作;程序几个函数的执行顺序是:
1、__init__
初始化界面,绑定按钮事件,然后进入循环等待用户输入;
2、点击确认后,执行process_input函数,该函数会继续调用process_data函数,返回数据后,显示到界面上;
说明
逻辑和Linux 命令行版差不多,只是输入的方式不一样,命令行是直接通过运行脚本后跟参数的方式,通过sys.argv[1]获取该参数;而GUI版本,则是通过tkinter模块的Entry组件获取输入,处理逻辑一样,不同的是添加了一个Button组件,需要对按钮的点击触发程序执行,并且将结果显示出来。运行效果
默认IP地址栏有IP,只需要点击确定即可:查询61.135.157.156的IP地址归属:
代码实现
#-*- coding: utf-8 -*- import tkinter import json import urllib.request import sys import ctypes class MYIP(): def __init__(self): #隐藏窗口 whnd = ctypes.windll.kernel32.GetConsoleWindow() if whnd != 0: ctypes.windll.user32.ShowWindow(whnd, 0) ctypes.windll.kernel32.CloseHandle(whnd) #main frame self.main_window = tkinter.Tk() #top frame self.top_frame = tkinter.Frame(self.main_window) #bottom frame self.bottom_frame = tkinter.Frame(self.main_window) self.L1 = tkinter.Label(self.top_frame, text=' IP 地址: ') self.L1.pack(side='left') default_value = tkinter.StringVar() default_value.set('8.8.8.8') self.E1 = tkinter.Entry(self.top_frame, textvariable=default_value, bd=2) self.E1.pack(side = tkinter.LEFT) self.B1 = tkinter.Button(self.top_frame, text='确定', command=self.prcess_input) self.B1.pack(side=tkinter.RIGHT) self.bottom_left = tkinter.Frame(self.bottom_frame) self.bottom_right = tkinter.Frame(self.bottom_frame) self.label1 = tkinter.Label(self.bottom_left,text="%-10s" % "国家") self.label2 = tkinter.Label(self.bottom_left,text="%-10s" % '区域') self.label3 = tkinter.Label(self.bottom_left,text="%-10s" % '地区') self.label4 = tkinter.Label(self.bottom_left,text="%-10s" % '城市') self.label5 = tkinter.Label(self.bottom_left,text="%-10s" % '县') self.label6 = tkinter.Label(self.bottom_left,text="%-10s" % '运营商') self.label1.pack(side='top') self.label2.pack(side='top') self.label3.pack(side='top') self.label4.pack(side='top') self.label5.pack(side='top') self.label6.pack(side='top') self.En1 = tkinter.Entry(self.bottom_right) self.En2 = tkinter.Entry(self.bottom_right) self.En3 = tkinter.Entry(self.bottom_right) self.En4 = tkinter.Entry(self.bottom_right) self.En5 = tkinter.Entry(self.bottom_right) self.En6 = tkinter.Entry(self.bottom_right) self.En1.pack(side='top') self.En2.pack(side='top') self.En3.pack(side='top') self.En4.pack(side='top') self.En5.pack(side='top') self.En6.pack(side='top') self.bottom_left.pack(side='left') self.bottom_right.pack(side='right') self.top_frame.pack() self.bottom_frame.pack() self.main_window.mainloop() def get_data(self): API = "http://ip.taobao.com/service/getIpInfo.php?ip=" url = API + self.ipaddr ret = (urllib.request.urlopen(url).read()).decode('utf-8') self.jsondata = (json.loads(ret)) def process_data(self): if self.jsondata['data']['country']: country = self.jsondata['data']['country'] else: country = "NULL" if self.jsondata['data']['area']: area = self.jsondata['data']['area'] else: area = "NULL" if self.jsondata['data']['region']: region = self.jsondata['data']['region'] else: region = "NULL" if self.jsondata['data']['city']: city = self.jsondata['data']['city'] else: city = "NULL" if self.jsondata['data']['county']: county = self.jsondata['data']['county'] else: county = "NULL" if self.jsondata['data']['isp']: isp= self.jsondata['data']['isp'] else: isp = "NULL" return (country, area, region, city, county, isp) def prcess_input(self): self.ipaddr = self.E1.get() myjson = self.get_data() if myjson == False: return 1 else: ret = self.process_data() self.En1.delete(0, tkinter.END) self.En2.delete(0, tkinter.END) self.En3.delete(0, tkinter.END) self.En4.delete(0, tkinter.END) self.En5.delete(0, tkinter.END) self.En6.delete(0, tkinter.END) self.En1.insert(0, ret[0]) self.En2.insert(0, ret[1]) self.En3.insert(0, ret[2]) self.En4.insert(0, ret[3]) self.En5.insert(0, ret[4]) self.En6.insert(0, ret[5]) myip = MYIP()
相关文章推荐
- python学习之编写查询ip程序
- Python中使用Tkinter模块创建GUI程序实例
- python学习之编写查询ip程序
- python-tkinter模块图形绘制、移动小程序(一)
- 制作通过IP 查询地址的java版程序
- python-tkinter模块图形绘制、移动小程序(二)
- python实现查询IP地址所在地
- Python中使用Tkinter模块创建GUI程序实例
- python查询IP地址所属地
- 制作通过IP 查询地址的java版程序
- python学习.编写查询ip程序 推荐
- python requests模块post请求(请求头+请求主体) 调用web接口批量查询ip所属地址
- python 相关模块安装 国内镜像地址
- python获取外网ip地址的方法总结
- Python中的getpass模块介绍(登陆程序)
- Python网络编程:接口地址查询
- 如何查询本机ip地址
- python查询IP对应的地理位置
- Python程序性能分析模块----------cProfile
- 有类别IP地址的程序分析