端口扫描工具(python)

最近在想学python边学边瞎捣鼓了一个端口扫描工具,功能很简单只有显示域名IP和显示端口,但只是简单的扫描端口没有做过滤,文件做了GUI处理编译成了.exe方便使用。我只是小白望大佬多多指教,下面是源码:

from socket import *
from tkinter import *
from urllib.parse import urlsplit
import threading
import time
from tkinter import END

win = Tk()
win.title("IP查询")
screenwidth = win.winfo_screenwidth()
screenheight = win.winfo_screenheight()
dialog_width = 300
dialog_height = 300
win.resizable(width=False, height=False)
font = ("宋体", 12, "bold",)
win.geometry(
    "%dx%d+%d+%d" % (dialog_width, dialog_height, (screenwidth - dialog_width) / 2, (screenheight - dialog_height) / 2))
str_1 = StringVar()
str_2 = ''
color = StringVar(value='red')
lock = threading.Lock()
openNum = 0
num = []
threads = []
adder_1 = StringVar(value=0)
adder_2 = StringVar(value=65535)


def test(content):
    # 如果不加上==""的话,就会发现删不完。总会剩下一个数字
    if content.isdigit() or content == "":
        return True
    else:
        return False


def hello():
    url = Entry_port.get()
    if not url.startswith('//', 6, 8) and not url.startswith('//', 5, 7):
        url = '//' + url
    r1 = urlsplit(url)
    lab_1 = r1.netloc
    global str_1
    global str_2
    if lab_1 != '':
        if (int(adder_1.get()) < 0) or (int(adder_1.get()) >= 65535) or (int(adder_2.get()) <= 0) or (
                int(adder_2.get()) > 65535):
            str_1.set('端口范围错误!')
            return False
        elif int(adder_1.get()) >= int(adder_2.get()):
            str_1.set('端口范围请从小到大!')
            return False
        else:
            try:
                ip = gethostbyname(lab_1)
                str_1.set(ip)
                str_2 = ip
                return True
            except Exception as e:
                str_1.set('目标无响应!')
                return False
    else:
        str_1.set('输入不能为空!')
        return False


def portScanner(host, port):
    global openNum
    try:
        s = socket(AF_INET, SOCK_STREAM)
        s.connect((host, port))
        lock.acquire()
        openNum += 1
        num.append('[+] %d 已开放' % port)
        print('[+] %d 已开放' % port)
        lock.release()
        s.close()
    except:
        print(port)
        pass


def main():
    global str_2
    global adder_1
    global adder_2
    global openNum
    global color
    if not hello():
        return
    color.set("green")
    print(color.get())
    num.clear()
    num.append('****************************************')
    num.append(str_2 + '========' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    setdefaulttimeout(1)
    add_1 = int(adder_1.get())
    add_2 = int(adder_2.get())
    for p in range(add_1, add_2):
        t = threading.Thread(target=portScanner, args=(str_2, p))
        threads.append(t)
        t.start()
    num.append('[*] 扫描完成!')
    num.append('[*] 共 %d 个开放端口 ' % openNum)
    num.append('****************************************')
    openNum = 0
    print(num)
    # num.reverse()
    # print(num)

    for item in num:
        listbox_port.insert(END, item)
    return
    # for t in threads:
    #     t.join()


test_cmd = win.register(test)

label_port = Label(win, text=" 域名:", font=font)
Entry_port = Entry(win)
btn_port = Button(win, text="查询", bg="white", command=main)
Entry_port_1 = Entry(win, width=5, justify='center', textvariable=adder_1, validate='key',
                     validatecommand=(test_cmd, '%P'))
label_port_1 = Label(win, text="--", font=font)
Entry_port_2 = Entry(win, width=5, justify='center', textvariable=adder_2)
text_port = Entry(win, width=30, font=font, bg="white", fg=color.get(), justify='center', textvariable=str_1,
                  validate='key',
                  validatecommand=(test_cmd, '%P'))
listbox_port = Listbox(win)
label_port.place(x=20, y=40)
Entry_port.place(x=80, y=40)
Entry_port_1.place(x=100, y=68)
label_port_1.place(x=140, y=68)
Entry_port_2.place(x=165, y=68)
btn_port.place(x=240, y=35)
listbox_port.pack(side=BOTTOM, fill=X)
text_port.pack(side=BOTTOM, fill=X)

win.mainloop()

运行后:

注释:域名后面填域名或者IP都可以,第二行为扫描端口范围,默认全端口扫描,可自行设置范围,点击“查询”开始扫描端口。扫描过程中窗口会进入“无响应”状态,但实际上后台还在扫描,扫描结束后就会回复正常,这个我不知道是为什么望大佬指教,扫描时常受网速和扫描端口范围影响,一般3分钟左右结束。 

编译成exe的连接:0积分下载。。

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值