有图形用户界面的python端口扫描程序
时间: 2023-12-26 13:04:56 浏览: 81
你可以使用Python的Tkinter模块创建一个图形用户界面的端口扫描程序。以下是一个示例代码:
```python
import socket
import tkinter as tk
class PortScanner:
def __init__(self, master):
self.master = master
master.title("Port Scanner")
self.label_host = tk.Label(master, text="Host:")
self.label_host.pack()
self.entry_host = tk.Entry(master)
self.entry_host.pack()
self.label_start = tk.Label(master, text="Start Port:")
self.label_start.pack()
self.entry_start = tk.Entry(master)
self.entry_start.pack()
self.label_end = tk.Label(master, text="End Port:")
self.label_end.pack()
self.entry_end = tk.Entry(master)
self.entry_end.pack()
self.button_scan = tk.Button(master, text="Scan", command=self.scan_ports)
self.button_scan.pack()
self.text_result = tk.Text(master)
self.text_result.pack()
def scan_ports(self):
host = self.entry_host.get()
start_port = int(self.entry_start.get())
end_port = int(self.entry_end.get())
self.text_result.delete("1.0", tk.END)
for port in range(start_port, end_port + 1):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
try:
s.connect((host, port))
self.text_result.insert(tk.END, f"Port {port} on {host} is open\n")
except socket.error:
self.text_result.insert(tk.END, f"Port {port} on {host} is closed\n")
s.close()
root = tk.Tk()
scanner = PortScanner(root)
root.mainloop()
```
该代码将创建一个简单的窗口,其中包含输入主机、起始端口和结束端口的文本框和扫描按钮。当用户点击扫描按钮时,程序将扫描指定主机上的指定端口范围,并将结果显示在文本框中。你可以根据需要修改代码来添加更多功能和改进用户界面。
阅读全文