如何获取大量子域的响应状态?

发布于 2025-01-13 17:40:24 字数 992 浏览 2 评论 0原文

我一直在尝试一次检查所有这些子域的状态,并且尝试了多种技术,甚至 grequests 和比 requests 更快的技术也没有多大帮助,然后我开始将 asyncio 与 aiohttp 一起使用,它现在比正常的 requests 库慢。我还检查了它实际上并不是异步发送请求,而是一个接一个地发送请求。

我知道“await resp.status”有问题,因为 resp.status 不支持等待,但我尝试删除它,但它仍然是一样的。

import aiohttp
import asyncio
import time

start_time = time.time()


async def main():
#List of 1000 subdomains , Some subdomains do not exist 

    data = [ "LIST OF 1000 SUBDOMAINS" ]

    async with aiohttp.ClientSession() as session:
        for url in data:
            pokemon_url = f'{url}'
            try:
                async with session.get(pokemon_url, ssl=False) as resp:
                    pokemon = await resp.status
               #If subdomain exists then print the status
                    print(pokemon)
            except:
               #else print the subdomain which does not exist or cannot be reached

                print(url)

asyncio.run(main())
print("--- %s seconds ---" % (time.time() - start_time))

I have been trying to check the status of all these subdomains all at once and I have tried multiple techniques even grequests and faster than requests wasn't much helpful and then I started using asyncio with aiohttp and it is slower than normal requests library now. Also I checked that it wasn't actually sending the requests asynchronously rather it was sending one after another.

I know that "await resp.status" has issues because resp.status does not support await but I tried removing it and it's still the same.

import aiohttp
import asyncio
import time

start_time = time.time()


async def main():
#List of 1000 subdomains , Some subdomains do not exist 

    data = [ "LIST OF 1000 SUBDOMAINS" ]

    async with aiohttp.ClientSession() as session:
        for url in data:
            pokemon_url = f'{url}'
            try:
                async with session.get(pokemon_url, ssl=False) as resp:
                    pokemon = await resp.status
               #If subdomain exists then print the status
                    print(pokemon)
            except:
               #else print the subdomain which does not exist or cannot be reached

                print(url)

asyncio.run(main())
print("--- %s seconds ---" % (time.time() - start_time))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲凉≈ 2025-01-20 17:40:24

我尝试了多种技术,甚至请求

grequests 对此也能很好地工作,如果您不想,则不必使用异步。

import grequests
import time

urls = ['https://httpbin.org/delay/4' for _ in range(4)]
# each of these requests take 4 seconds to complete
# serially, these would take at least 16 (4 * 4) seconds to complete

reqs = [grequests.get(url) for url in urls]
start = time.time()
for resp in grequests.imap(reqs, size=4):
    print(resp.status_code)
end = time.time()
print('finished in', round(end-start, 2), 'seconds')
200
200
200
200
finished in 4.32 seconds

I have tried multiple techniques even grequests

grequests works fine for this, you don't have to use async if you don't want.

import grequests
import time

urls = ['https://httpbin.org/delay/4' for _ in range(4)]
# each of these requests take 4 seconds to complete
# serially, these would take at least 16 (4 * 4) seconds to complete

reqs = [grequests.get(url) for url in urls]
start = time.time()
for resp in grequests.imap(reqs, size=4):
    print(resp.status_code)
end = time.time()
print('finished in', round(end-start, 2), 'seconds')
200
200
200
200
finished in 4.32 seconds
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文