第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

python asyncio aiohttp 超時(shí)

python asyncio aiohttp 超時(shí)

MMTTMM 2024-01-04 10:42:00
注意事項(xiàng):這是我第一次使用 asyncio,所以我可能做了一些非常愚蠢的事情。場(chǎng)景如下:我需要“http-ping”一個(gè)巨大的 url 列表來(lái)檢查它們是否響應(yīng) 200 或任何其他值。盡管像 gobuster report 200,403 等工具,我對(duì)每個(gè)請(qǐng)求都會(huì)超時(shí)。我的代碼與此類似:import asyncio,aiohttpimport 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   我真的做錯(cuò)了什么嗎?有什么建議嗎?
查看完整描述

2 回答

?
海綿寶寶撒

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊

實(shí)際上,我最終在 aio-libs/aiohttp 中發(fā)現(xiàn)了一個(gè)未解決的問(wèn)題: 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:

       ...


查看完整回答
反對(duì) 回復(fù) 2024-01-04
?
喵喔喔

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊

回答你的問(wèn)題 - 不,你沒(méi)有做錯(cuò)任何事。在 http 請(qǐng)求/響應(yīng)/超時(shí)處理方面,我看不出您的代碼有任何問(wèn)題。

如果確實(shí)您的所有請(qǐng)求都超時(shí)到主機(jī)(

我的小更新(下面的代碼)是輸出。

http://127.0.0.1:8080/.bashrc.php

#404

http://127.0.0.1:8080/.bashrc

#404

http://127.0.0.1:8080/.bashrc.html

#404

http://127.0.0.1:8080/abc

#HIT? ?2020-11-03 12:57:33 200? http://127.0.0.1:8080/abc

http://127.0.0.1:8080/zt.php

#404

http://127.0.0.1:8080/zt.html

#404

http://127.0.0.1:8080/zt

#404

http://127.0.0.1:8080/abc.html

#HIT? ?2020-11-03 12:57:33 200? http://127.0.0.1:8080/abc.html

http://127.0.0.1:8080/abc.php

#404

DONE

我的更新大部分都很小,但可能有助于進(jìn)一步調(diào)試。

import asyncio

import aiohttp

import datetime



async def get_data_coroutine(session, url, follow_redirects, timeout_seconds, retries):

? ? try:

? ? ? ? async with session.get(

? ? ? ? ? ? url, allow_redirects=False, timeout=timeout_seconds

? ? ? ? ) as response:

? ? ? ? ? ? print(url)

? ? ? ? ? ? now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

? ? ? ? ? ? if response.ok:

? ? ? ? ? ? ? ? print(f"#HIT? ?{now} {response.status}? {url}")

? ? ? ? ? ? else:

? ? ? ? ? ? ? ? status = response.status

? ? ? ? ? ? ? ? if status == 404:

? ? ? ? ? ? ? ? ? ? print("#404")

? ? ? ? ? ? ? ? elif 300 <= status and status < 400:

? ? ? ? ? ? ? ? ? ? location = str(response).split("Location': '")[1].split("'")[0]

? ? ? ? ? ? ? ? ? ? print(f"#HIT? ?{now}? {status} {url} ---> {location}")

? ? ? ? ? ? ? ? ? ? if follow_redirects is True:

? ? ? ? ? ? ? ? ? ? ? ? return await get_data_coroutine(

? ? ? ? ? ? ? ? ? ? ? ? ? ? session, location, follow_redirects, timeout_seconds, retries

? ? ? ? ? ? ? ? ? ? ? ? )

? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? print("#ERROR ", response.status)

? ? ? ? ? ? return None

? ? except asyncio.TimeoutError as e:

? ? ? ? now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

? ? ? ? print(f"#ERROR? ?{now} {url} TIMEOUT ", e)

? ? ? ? return None



async def main(loop):

? ? base_url = "http://127.0.0.1:8080"

? ? extensions = ["", ".html", ".php"]

? ? fd = open("/usr/share/wordlists/dirb/common.txt", "r")

? ? words_without_suffix = [x.strip() for x in fd.readlines()]

? ? 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))


查看完整回答
  • 2 回答
  • 0 關(guān)注
  • 262 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)