Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python and 3 aiohttp to find the URL after redirect when timeout

I'm currently trying to audit a large number of redirect URL handles to make sure that their destinations are still valid.

I'm using aiohttp to go through the large volume in order to produce a report.

try:
    with aiohttp.Timeout(timeout):
        async with session.get(url) as resp:
            return {"Handle URL": url,
                    "Status Code": resp.status,
                    "Redirects": resp.url != url,
                    "Resolving URL": resp.url,
                    "Success": resp.status == 200,
                    "Message": ""}
except asyncio.TimeoutError:
        return {"Handle URL": url,
                "Success": False,
                "Message": "Handle server timed out. >{} seconds".format(timeout)}

For the most part, this has been fine for identifying which URL redirect no longer sends to a valid URL. However, I'd really like to know the final address where times out.

Any ideas?

like image 623
loneraver Avatar asked Oct 26 '25 01:10

loneraver


1 Answers

The only way to do it is disabling redirects by allow_redirects=False and performing redirections manually.

like image 70
Andrew Svetlov Avatar answered Oct 27 '25 16:10

Andrew Svetlov