【问题标题】:How to check the status code of an Async Http Session Request如何检查异步 Http 会话请求的状态代码
【发布时间】:2023-02-17 01:37:06
【问题描述】:
async def review(url, session, **kwargs):

try:
        #this checks the method from the list of ARGS kinda deleted the code 
    if method == 'GET':


        async with session.get(url) as response:

                # i did some printing in the code after the if condition but it was not executed 
            if await response.status_code == 200:
                print('[bold green]`[+]` [default]Target is responsive')
                print(response.data)
                return await response


except Exception as err:
    print(f'[red]ERROR {err} Target {url} is not stable ')
        url_obj['stable'] = False

else:
    url_obj['stable'] = True
    status = response.status_code
finally:
    if url_obj.get('stable') is True:
        return f'Target is stable'
    else:
        return f'Target is unstable status code {status}'


async def main(url, **kwargs):
    async with ClientSession() as session:
        data= await review(URL, session, **kwargs)
            print(data)
           

def probe(url, **kwargs):
    asyncio. run(main(url,**kwargs))

导入此代码并运行时出现此错误

ERROR: 'ClientResponse' object has no attribute 'status_code'目标http://localhost:8080/site.html不稳定 追溯(最近一次通话):

需要注意的是,我在本地托管了该网站,并且我收到了浏览器的响应 我想我在 await response.status_code 中遗漏了一些东西或者没有正确实现它。

【问题讨论】:

    标签: python python-3.x session async-await aiohttp


    【解决方案1】:

    尝试将 status_code 更改为 status - 看起来这就是它在 ClientSession 类中的名称。

    【讨论】:

      猜你喜欢
      • 2020-12-20
      • 1970-01-01
      • 2018-07-31
      • 2010-12-26
      • 2022-06-18
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      • 1970-01-01
      相关资源
      最近更新 更多