Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check url status code using aiohttp?

I have the following code to check url status code:

async def _is_url_ok(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            return resp.status == 200

What is the best way to handle an exception? Is it ok to do like this?

async def _is_url_ok(url):
    try:
       async with aiohttp.ClientSession() as session:
           async with session.get(url) as resp:
               return resp.status == 200
    except:
        return False
like image 583
petrush Avatar asked Oct 17 '25 04:10

petrush


1 Answers

It depends on what is your target.

Sometimes you need a page content validation etc.

Anyway catching aiohttp.ClientError is better than bare except.

like image 143
Andrew Svetlov Avatar answered Oct 18 '25 20:10

Andrew Svetlov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!