Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. async def ping_url(session, url):
  2. logger.debug('Ping: %s', url)
  3. try:
  4. response = await session.get(url)
  5. except asyncio.TimeoutError:
  6. logger.info('Ping timeout for %s', url)
  7. return ('TIMEOUT', url)
  8. else:
  9. logger.debug('Ping done for %s', url)
  10. return (response.status, url)
  11.  
  12.  
  13. async def ping_urls(urls, headers=None):
  14. async with aiohttp.ClientSession(timeout=timeout, headers=headers) as session:
  15. tasks = [
  16. asyncio.create_task(
  17. ping_url(session, url)
  18. ) for url in urls
  19. ]
  20. ping_results = await asyncio.gather(*tasks)
  21.  
  22. return ping_results
  23.  
  24. async def ping_url(session, url):
  25. logger.debug('Ping: %s', url)
  26. try:
  27. response = await session.get(url)
  28. except requests_async.exceptions.Timeout:
  29. logger.info('Ping timeout for %s', url)
  30. return ('TIMEOUT', url)
  31. else:
  32. logger.debug('Ping done for %s', url)
  33. return (response.status_code, url)
  34.  
  35.  
  36. async def ping_urls(urls, headers=None):
  37. async with requests_async.Session() as session:
  38. session.headers.update(headers or {})
  39. session.timeout = 2
  40. tasks = [
  41. asyncio.create_task(
  42. ping_url(session, url)
  43. ) for url in urls
  44. ]
  45. ping_results = await asyncio.gather(*tasks)
  46.  
  47. return ping_results
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement