Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async def ping_url(session, url):
- logger.debug('Ping: %s', url)
- try:
- response = await session.get(url)
- except asyncio.TimeoutError:
- logger.info('Ping timeout for %s', url)
- return ('TIMEOUT', url)
- else:
- logger.debug('Ping done for %s', url)
- return (response.status, url)
- async def ping_urls(urls, headers=None):
- async with aiohttp.ClientSession(timeout=timeout, headers=headers) as session:
- tasks = [
- asyncio.create_task(
- ping_url(session, url)
- ) for url in urls
- ]
- ping_results = await asyncio.gather(*tasks)
- return ping_results
- async def ping_url(session, url):
- logger.debug('Ping: %s', url)
- try:
- response = await session.get(url)
- except requests_async.exceptions.Timeout:
- logger.info('Ping timeout for %s', url)
- return ('TIMEOUT', url)
- else:
- logger.debug('Ping done for %s', url)
- return (response.status_code, url)
- async def ping_urls(urls, headers=None):
- async with requests_async.Session() as session:
- session.headers.update(headers or {})
- session.timeout = 2
- tasks = [
- asyncio.create_task(
- ping_url(session, url)
- ) for url in urls
- ]
- ping_results = await asyncio.gather(*tasks)
- return ping_results
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement