小编hum*_*mid的帖子

使用 asyncio/aiohttp 返回 404 响应时遇到问题

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 …
Run Code Online (Sandbox Code Playgroud)

python-3.x python-asyncio aiohttp

-3
推荐指数
1
解决办法
1308
查看次数

标签 统计

aiohttp ×1

python-3.x ×1

python-asyncio ×1