欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python socket 编写普通端口扫描器

程序员文章站 2022-05-15 22:09:04
...

话不多说,上代码(原理:利用socket套接字挨个连接目标端口,成功则输出open,失败则跳过)

from socket import *
import time

def ping():
    ping = os.system('ping')
    print(ping)

def handle(ip,port):
    try:
        client = socket(AF_INET,SOCK_STREAM)
        #client = socket()  默认TCP
        client.settimeout(2)  #设置超时两秒,超过就默认关闭
        client.connect((ip,port))
        print(f"{ip}:{port}open",end = '\n') 
    except:
        # print(f"{port}不开放")
        pass
    client.close()

if __name__ == "__main__":
    ip = "43.248.190.4"
    list_port = [20,21,22,23,25,53,80,81,102,109,110,119,135,137,138,139,161,443,554,1024,1080,1755,4000,5554,5632,8080] #一般端口
    for i in list_port:
        print(f"正在扫描:{i}",end = '\r')#不断刷新当行
        handle(ip,i)
                                    **^蕾姆(* ̄(oo) ̄)镇楼^**

python socket 编写普通端口扫描器