如何使用 aiohttp 检查 url 状态码?
How to check url status code using aiohttp?
我有以下代码来检查 url 状态码:
async def _is_url_ok(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
return resp.status == 200
处理异常的最佳方法是什么?这样可以吗?
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
这取决于你的目标是什么。
有时您需要页面内容验证等
总之抓aiohttp.ClientError
总比裸except
强。
我有以下代码来检查 url 状态码:
async def _is_url_ok(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
return resp.status == 200
处理异常的最佳方法是什么?这样可以吗?
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
这取决于你的目标是什么。
有时您需要页面内容验证等
总之抓aiohttp.ClientError
总比裸except
强。