当前位置:首页 > 运维 > 正文内容

Windows平台Python aiodns模块异步代码无法执行

MuWinds1个月前 (02-09)运维18

首先放一下原先的异步函数代码:

@staticmethod
async def check_subdomain(domain: str):
    """异步子域名枚举"""
    resolver = aiodns.DNSResolver()
    found = []
    try:
        with open("app/1knames.txt") as f:
            subdomains = [line.strip() for line in f]
    except FileNotFoundError:
        print("Subdomain dictionary not found")
        return False        
    semaphore = asyncio.Semaphore(500)  # 限制并发量
   
    async def _query(sub: str):
        async with semaphore:
            full_domain = f"{sub}.{domain}"
            print(full_domain)
            try:
                a_records = await resolver.query(full_domain, 'A')
                if a_records:
                    found.append(full_domain)
                    return
            except aiodns.error.DNSError:
                pass
            try:
                cname_records = await resolver.query(full_domain, 'CNAME')
                if cname_records:
                    found.append(full_domain)
            except aiodns.error.DNSError:
                pass
    batch_size = 2000
    for i in range(0, len(subdomains), batch_size):
        batch = subdomains[i:i+batch_size]
        tasks = [_query(sub) for sub in batch]
        await asyncio.gather(*tasks)
    assets_table = AssetsTable(host=domain,subdomain=json.dumps(found))
    assets_info = AssetsInfo(assets_table)
    assets_info.update_host_info()
    return True

乍一看没啥问题,但是如果在windows下跑起来就会发现啥输出都没有,如果一步步调试就会神奇的发现卡在第一行初始化

aiodns是异步dns模块,运行aiodns相关的函数不仅需要写成异步的形式,在windows下还需要更改asyncio的异步运行模式

在函数前面加一行就可以解决:

 asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())


返回列表

上一篇:简单理解k8s资源限制参数中cpu计量单位

没有最新的文章了...

“Windows平台Python aiodns模块异步代码无法执行” 的相关文章

完美永久破解最新Sublime Text 4 Build 4107 (Windows版)

1、下载并安装Sublime Text 4 Build 4107;官方下载地址:Windows 64位: https://download.sublimetext.com/sublime_text_build_4107_x64_setup.exe 2、 使用浏览器打开网站:https://hexed...

OpenWrt无法保存配置无法生效的解决方法

原因:意外断电导致硬盘卡在只读状态进入openwrt后台按回车输入下面命令,重新挂载即可:mount -o remount rw /2024.07.10更新:换了官方固件后因为要拆下来换机箱散热器又是一次意外断电,卡在了只读状态,但是这次重新挂载无用了。现在这...

CentOS Python后台运行

nohup python /data/python/server.py > python.log3 2>&1 &说明:1、1是标准输出(STDOUT)的文件描述符,2是标准错误(STDERR)的文件描述符 &nb...

CentOS设置开机启动

AIO出现了硬盘问题,重启后发现离线下载服务没有开机自启,快速给开一下:先写一个开机自启的脚本:#!/bin/sh #chkconfig: 2345 80 90 #description:aria2开机自启 aria2c --conf-path=/e...

node-saas问题

构建vue项目的时候报错:很简单,nodejs版本太新了,直接换到dart-sassyarn remove node-sass yarn add sass...

在自己的家里建立属于自己的防火长城

终于明白了伟大的江泽民、胡锦涛与习近平元帅同志为啥要设立防火长城这个违背宪法中通信自由的东西了,我妈天天刷抖音快手,有的时候我一起来听到各种营销号对我耳朵的轰炸我又直接昏睡过去。以下说一下方法,我个人用的x86的openwrt,仅供参考:在百度、谷歌轮番搜索,搜到一个openwrt的过滤插件:FRO...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。