DeepSeek开发IP地址查询小工具
- 其他
- 2025-09-21 04:42:02

一、开发GUI
以下是用 Python 和 Tkinter 实现的带图形界面:
import requests import tkinter as tk from tkinter import ttk, messagebox import json class IPLookupApp: def __init__(self, root): self.root = root root.title("IP 地址查询工具") root.geometry("500x450") # 创建界面元素 self.create_widgets() def create_widgets(self): # 输入区域 input_frame = ttk.Frame(self.root, padding=10) input_frame.pack(fill=tk.X) self.lbl_ip = ttk.Label(input_frame, text="目标 IP 地址:") self.lbl_ip.grid(row=0, column=0, padx=5) self.entry_ip = ttk.Entry(input_frame, width=25) self.entry_ip.grid(row=0, column=1, padx=5) self.btn_lookup = ttk.Button(input_frame, text="查询", command=self.do_lookup) self.btn_lookup.grid(row=0, column=2, padx=5) # 结果显示区域 result_frame = ttk.LabelFrame(self.root, text="查询结果", padding=10) result_frame.pack(pady=10, fill=tk.BOTH, expand=True) columns = ("属性", "值") self.result_tree = ttk.Treeview( result_frame, columns=columns, show="headings", height=10 ) self.result_tree.heading("属性", text="属性") self.result_tree.heading("值", text="值") self.result_tree.column("属性", width=120, anchor=tk.W) self.result_tree.column("值", width=300, anchor=tk.W) vsb = ttk.Scrollbar(result_frame, orient="vertical", command=self.result_tree.yview) self.result_tree.configure(yscrollcommand=vsb.set) self.result_tree.grid(row=0, column=0, sticky=tk.NSEW) vsb.grid(row=0, column=1, sticky=tk.NS) result_frame.grid_rowconfigure(0, weight=1) result_frame.grid_columnconfigure(0, weight=1) def do_lookup(self): """执行查询操作""" ip = self.entry_ip.get().strip() or None self.result_tree.delete(*self.result_tree.get_children()) try: data = get_ip_info(ip) if data and data.get("status") == "success": self.show_results(data) else: messagebox.showerror("错误", data.get("message", "查询失败")) except Exception as e: messagebox.showerror("异常", f"发生错误: {str(e)}") def show_results(self, data): """在 Treeview 中展示结果""" results = [ ("IP 地址", data.get("query")), ("地理位置", f"{data.get('continent', 'N/A')} - {data.get('country', 'N/A')}"), ("地区", f"{data.get('regionName', 'N/A')} {data.get('city', 'N/A')}"), ("经纬度", f"[{data.get('lat', 'N/A')}, {data.get('lon', 'N/A')}]"), ("时区", data.get("timezone", "N/A")), ("ISP", data.get("isp", "N/A")), ("AS 编号", data.get("as", "N/A")), ("移动网络", str(data.get("mobile", False))), ("VPN/代理", str(data.get("proxy", False))), ("托管服务", str(data.get("hosting", False))), ] for item in results: self.result_tree.insert("", tk.END, values=item) def get_ip_info(ip_address=None): """查询 IP 地址信息(复用原有函数)""" url = f'http://ip-api /json/{ip_address or ""}' params = {'fields': 'status,message,continent,country,regionName,city,lat,lon,timezone,isp,as,mobile,proxy,hosting,query'} try: response = requests.get(url, params=params, headers={'User-Agent': 'python-requests/2.25.1'}) return response.json() except Exception as e: raise RuntimeError(f"API 请求失败: {str(e)}") if __name__ == "__main__": root = tk.Tk() app = IPLookupApp(root) root.mainloop()主要内容:
二、将 Python 程序打包成 EXE 相关资源
步骤 1:安装打包工具 pip install pyinstaller 步骤 2:创建打包配置文件(可选)
在代码目录新建 build.spec 文件:
# -*- mode: python ; coding: utf-8 -*- block_cipher = None a = Analysis( ['ip_lookup_gui.py'], pathex=[], binaries=[], datas=[], hiddenimports=[], hookspath=[], hooksconfig={}, runtime_hooks=[], excludes=[], win_no_prefer_redirects=False, win_private_assemblies=False, cipher=block_cipher, noarchive=False, ) pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) exe = EXE( pyz, a.scripts, a.binaries, a.zipfiles, a.datas, [], name='IPLookupTool', debug=False, bootloader_ignore_signals=False, strip=False, upx=True, upx_exclude=[], runtime_tmpdir=None, console=False, # 不显示控制台窗口 icon='icon.ico', # 可选图标文件 disable_windowed_traceback=False, argv_emulation=False, target_arch=None, codesign_identity=None, entitlements_file=None, ) 步骤 3:执行打包命令 pyinstaller --noconsole --onefile ip_lookup_gui.py或使用配置文件:
pyinstaller build.spec打包优化技巧
添加程序图标:
准备 icon.ico 图标文件在命令后追加:--icon=icon.ico减小体积:
pyinstaller --noconsole --onefile --clean --upx-dir=/path/to/upx ip_lookup_gui.py排除无用组件:
--exclude-module matplotlib --exclude-module numpy最终命令:
pyinstaller --noconsole --onefile --clean --icon=icon.ico --exclude-module matplotlib --exclude-module numpy --upx-dir=./upx ip_lookup_gui.py生成文件说明 dist/ └── IPLookupTool.exe # 最终生成的独立可执行文件
使用注意事项
文件大小:
原始大小约 8-15MB(使用 UPX 压缩后)未压缩版本约 20-30MB安全软件警报:
首次运行可能触发杀毒软件警告(误报),需手动允许网络权限:
确保程序有权限访问网络(Windows 防火墙设置)最终效果
用户将获得一个双击即可运行的 IPLookupTool.exe 文件:
[ 简洁的 GUI 界面 ] ↳ 输入框自动支持 IP 格式检测 ↳ 查询结果表格支持右键复制 ↳ 网络异常时弹出友好提示常见问题解决
问题 1:打包时报错 ModuleNotFoundError
解决方案:通过 --hidden-import=模块名 手动添加缺失模块问题 2:生成的 EXE 闪退
调试方法:pyinstaller --debug all ip_lookup_gui.py问题 3:需要兼容旧系统
添加参数:--target-arch=win32通过这种方式打包的工具可以零依赖运行于 Windows 7/8/10/11 系统,适合非技术用户使用。如果要分发程序,建议使用 Inno Setup 或 NSIS 制作安装包。
DeepSeek开发IP地址查询小工具由讯客互联其他栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“DeepSeek开发IP地址查询小工具”
上一篇
如何杀死僵尸进程?没有那个进程?
下一篇
小结:计算机网路中的性能指标小结