Python scans urls in batches (asynchronous multi-threaded processing) to detect whether there is a website deployment

the code

注释写的很小白了,看代码

# 编写者:富贵满堂喜盈门
# 目的:进行python脚本编写
# 开发时间:2023/2/16 12:55
# 此项目目的:针对某特定端口或范围IP进行扫描并确定是否部署网站



import asyncio # 异步多线程处理
import os      # 当前操作系统,主要用来辨识路径用的
import aiohttp # 异步处理http链接问题
import warnings # 用来处理警告
import IPy # 拆分地址块成IP地址,这个包处理ip地址的

warnings.filterwarnings("ignore") # 警告信息忽略

# print(os.getcwd())  # 获取工作目录
if os.path.exists('./scan_result') == 0: # 创建文件夹 ./代表的是你当前项目的工作路径,文件夹和你的。py文件同一个文件夹
    os.makedirs('./scan_result')

def get_IP(ip_address_block): # 处理IP地址块
    filename = './scan_result/NoScanIP.txt'
    filename_NoScanIP = open(filename, mode="a") # 打开文件并追加
    ip = IPy.IP(ip_address_block)    # 自动处理成ip
    for i in ip:
        filename_NoScanIP.write(str(i)+'\n') # 将ip写入文件

async def scan_ip(ip):
    filename = './scan_result/Life_IP.txt' # 没有自动创建
    filename_LifeIP = open(filename, mode="a")  # 打开文件并追加
    url="http://"+ip #这个地方来确定你扫描的url,看个人修改
    print(url) #
    try:
        async with  aiohttp.ClientSession() as session: # 异步链接
            async with await session.get(url,timeout=2) as resp:      # 回应报文,await触发,进行下一个函数操作
                if  len(resp.text) > 8000: # 根据自己想要的条件过滤网页,我是看这个地方部署的有没有网站,有网站且长度大于8000就进行下去
                    filename_LifeIP.write(str(ip)+'\n') #把ip保存下来
                    return ip
    except Exception as e:
        return ip



if __name__ == '__main__': # 防止引用代码出现错误
    # ip地址处理
    with open(r"C:/Users/1/Desktop/Test/ChineseIP - 1.txt", mode='r') as ip_address_block_line: # 这个‘r’是为了确定绝对路径被正确读取
        for line in ip_address_block_line:
            get_IP(line)
    # 筛选符合条件ip
    with open("./scan_result/NoScanIP.txt",mode='r') as ip_address:
        for line in ip_address:
            asyncio.run(scan_ip(line))

Summarize

Give it a thumbs up, goodbye!

Guess you like

Origin blog.csdn.net/qq_57223070/article/details/129064272