import socket
import tkinter.messagebox
from tkinter import *
class portScan:
def __init__(self):
window = Tk()
window.title("端口扫描")
frame = Frame(window)
frame.pack()
Label(frame, text="IP地址、域名 :").grid(row=1, column=1, sticky=E)
self.ipName = StringVar()
Entry(frame, textvariable=self.ipName).grid(row=1, column=2)
Label(frame, text="起始端口(最小为1):").grid(row=2, column=1, sticky=E)
self.startPort = IntVar()
Entry(frame, textvariable=self.startPort).grid(row=2, column=2)
Label(frame, text="结束端口(最大为65535):").grid(row=3, column=1, sticky=E)
self.endPort = IntVar()
Entry(frame, textvariable=self.endPort).grid(row=3, column=2)
Button(frame, text="扫描", command=self.getPortScan).grid(row=4, column=1, sticky=E)
self.text = Text(window)
self.text.pack()
window.mainloop()
def getPortScan(self):
try:
if (self.startPort.get() < 0 or self.endPort.get() > 65535):
tkinter.messagebox.showerror("error", "请输入正确的端口号!")
elif (self.startPort.get() > self.endPort.get()):
tkinter.messagebox.showerror("error", "请输入正确的端口号!")
else:
self.text.delete("1.0", "end")
for port in range(self.startPort.get(), self.endPort.get() + 1):
try:
s = socket.socket()
s.settimeout(1)
s.connect((str(self.ipName.get()), int(port)))
self.text.insert(END, self.ipName.get() + ":" + str(port) + "已开放\n")
except socket.error:
self.text.insert(END, self.ipName.get() + ":" + str(port) + "未开放\n")
finally:
s.close()
self.text.insert(END, "扫描完成")
except tkinter.TclError:
tkinter.messagebox.showerror("error", "请输入正确的端口号!")
portScan()
暂无评论