使用asyncio / aiohttp无法返回404响应

问题描述 投票:1回答:1
import time
import asyncio
import aiohttp

async def is_name_available(s, name):
    async with s.get("https://twitter.com/%s" % name) as res:
        if res.raise_for_status == 404:
            print('%s is available!' % name)
            return name

async def check_all_names(names):
    async with aiohttp.ClientSession(raise_for_status=True) as s:
        tasks = []
        for name in names:
            task = asyncio.create_task(is_name_available(s, name))
            tasks.append(task)
        return await asyncio.gather(*tasks)

def main():    
    with open('names.txt') as in_file, open('available.txt', 'w') as out_file:        
        names = [name.strip() for name in in_file]
        start_time = time.time()
        results = asyncio.get_event_loop().run_until_complete(check_all_names(names))
        results = [i for i in results if i]
        out_file.write('\n'.join(results))
        print(f'[ <? ] Checked {len(names)} words in {round(time.time()-start_time, 2)} second(s)')

if __name__ == '__main__':
    main()

我似乎无法弄清楚如何从我的另一个项目中使用的这个asyncio / aiohttp结构中,仅返回is_name_available中的404链接。我是python的初学者,对您的帮助表示赞赏。

python-3.x python-asyncio aiohttp
1个回答
2
投票

此行不正确:

        if res.raise_for_status == 404:

raise_for_status方法,因此您应该调用它,而不是将它与数字进行比较(该数字将始终返回false)。在您的情况下,您不想一开始就调用raise_for_status,因为您不想在遇到404时引发异常,但是要检测到它。要检测404,您可以简单地编写:

        if res.status == 404:

还请注意,您不想指定raise_for_status=True,因为它会在if有运行机会之前引发404的异常。

© www.soinside.com 2019 - 2024. All rights reserved.