python asyncio aiohttp超时

格列佐

注意:这是我使用asyncio的第一个方法,因此我可能做过一些非常愚蠢的事情。

方案如下:

我需要“ http-ping”庞大的URL列表,以检查它们是否响应200或任何其他值。我会收到每个请求的超时信息,尽管gobuster报告200,403之类的工具等等。

我的代码与此类似:

import asyncio,aiohttp
import datetime 
#-------------------------------------------------------------------------------------
async def get_data_coroutine(session,url,follow_redirects,timeout_seconds,retries):
    #print('#DEBUG '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+' '+url)
    try:
        async with session.get(url,allow_redirects=False,timeout=timeout_seconds) as response:
            status  =   response.status
            #res     =   await response.text()
            if(  status==404):
                pass
            elif(300<=status and status<400):
                location = str(response).split("Location': \'")[1].split("\'")[0]
                print('#HIT   '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+' '+str(status)+' '+url+' ---> '+location)
                if(follow_redirects==True):
                    return await get_data_coroutine(session,location,follow_redirects,timeout_seconds,retries)
            else:
                print('#HIT   '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+' '+str(status)+' '+url)
            return None
    except asyncio.exceptions.TimeoutError as e:
        print('#ERROR '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+' '+'   '+' '+url+' TIMEOUT '+str(e))
        return None
#---------------------------------------------------------------------------    
async def main(loop):
        base_url                =   'http://192.168.59.37'
        extensions              =   ['','.html','php']
        fd                      =   open('/usr/share/wordlists/dirb/common.txt','r')
        words_without_suffix    =   [x.strip() for x in fd.readlines()]#[-5:] #DEBUG!
        words_with_suffix       =   [base_url+'/'+x+y for x in words_without_suffix for y in extensions]
        follow                  =   True
        total_timeout           =   aiohttp.ClientTimeout(total=60*60*24)
        timeout_seconds         =   10
        retries                 =   1
        async with aiohttp.ClientSession(loop=loop,timeout=total_timeout) as session:
            tasks = [get_data_coroutine(session,url,follow,timeout_seconds,retries) for url in words_with_suffix]
            await asyncio.gather(*tasks)
        print('DONE')
#---------------------------------------------------------------------------    
if(__name__=='__main__'):
    loop    =   asyncio.get_event_loop()
    result  =   loop.run_until_complete(main(loop))
   

我做错什么了吗?

有什么建议吗?

非常感谢!

格列佐

实际上,我最终在aio-libs / aiohttp中找到了一个未解决的问题:https : //github.com/aio-libs/aiohttp/issues/3203

通过这种方式,他们提出了一种满足我需求的解决方法:

session_timeout =   aiohttp.ClientTimeout(total=None,sock_connect=timeout_seconds,sock_read=timeout_seconds)
async with aiohttp.ClientSession(timeout=session_timeout) as session:
    async with session.get(url,allow_redirects=False,timeout=1) as response:
       ...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Python中使用aiohttp或asyncio创建并行循环?

来自分类Dev

Python asyncio aiohttp 请求数据编码

来自分类Dev

在python 3.8中使用aiohttp和asyncio关闭异常事件循环

来自分类Dev

如何由于超时而从已取消的python asyncio协程返回值

来自分类Dev

python aiohttp超时是用于单个TCP连接还是用于HTTP请求?

来自分类Dev

asyncio/aiohttp 不返回响应

来自分类Dev

Python 3 asyncio-vs asyncio的收益

来自分类Dev

Python asyncio简单示例

来自分类Dev

Python的asyncio同步工作

来自分类Dev

Python asyncio调试示例

来自分类Dev

发送邮件python asyncio

来自分类Dev

asyncio(python)如何工作?

来自分类Dev

理解python中的asyncio

来自分类Dev

Python asyncio 等待任务

来自分类Dev

如何使用asyncio添加连接超时?

来自分类Dev

asyncio等待多个任务超时和取消

来自分类Dev

asyncio在超时时终止子进程

来自分类Dev

asyncio python 3.6 代码到 asyncio python 3.4 代码

来自分类Dev

使用Slack bot的Python asyncio

来自分类Dev

python asyncio无效语法ubuntu

来自分类Dev

Python asyncio协议竞争条件

来自分类Dev

Python asyncio程序不会退出

来自分类Dev

将Python Cement与asyncio结合

来自分类Dev

Python unittest + asyncio永久挂起

来自分类Dev

Python asyncio wait_for同步

来自分类Dev

将Python Cement与asyncio结合

来自分类Dev

AsyncIO python3.4 restserver

来自分类Dev

python asyncio无效语法ubuntu

来自分类Dev

Python asyncio队列未更新