Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. import asyncio
  2.  
  3. import aiohttp
  4.  
  5.  
  6. @asyncio.coroutine
  7. def is_html(session, url):
  8. response = yield from session.head(url, compress=True)
  9. print(url, "text/html" in response.headers["Content-Type"])
  10.  
  11.  
  12. if __name__ == '__main__':
  13. links = ["https://httpbin.org/html",
  14. "https://httpbin.org/image/png",
  15. "https://httpbin.org/image/svg",
  16. "https://httpbin.org/image"]
  17. loop = asyncio.get_event_loop()
  18.  
  19. conn = aiohttp.TCPConnector(verify_ssl=False)
  20. with aiohttp.ClientSession(connector=conn, loop=loop) as session:
  21. f = asyncio.wait([is_html(session, link) for link in links])
  22. loop.run_until_complete(f)
  23.  
  24. https://httpbin.org/image/svg False
  25. https://httpbin.org/image False
  26. https://httpbin.org/image/png False
  27. https://httpbin.org/html True
  28.  
  29. import asyncio
  30. import aiohttp
  31. URLS = [...]
  32.  
  33. if __name__ == "__main__":
  34. print(
  35. asyncio.get_event_loop().run_until_complete(
  36. asyncio.gather(*(foo(url) for url in URLS))))
  37.  
  38. async def foo(url):
  39. async with aiohttp.ClientSession() as s:
  40. async with s.head(...) as r:
  41. return url, r.headers[...]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement