Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- import aiohttp
- @asyncio.coroutine
- def is_html(session, url):
- response = yield from session.head(url, compress=True)
- print(url, "text/html" in response.headers["Content-Type"])
- if __name__ == '__main__':
- links = ["https://httpbin.org/html",
- "https://httpbin.org/image/png",
- "https://httpbin.org/image/svg",
- "https://httpbin.org/image"]
- loop = asyncio.get_event_loop()
- conn = aiohttp.TCPConnector(verify_ssl=False)
- with aiohttp.ClientSession(connector=conn, loop=loop) as session:
- f = asyncio.wait([is_html(session, link) for link in links])
- loop.run_until_complete(f)
- https://httpbin.org/image/svg False
- https://httpbin.org/image False
- https://httpbin.org/image/png False
- https://httpbin.org/html True
- import asyncio
- import aiohttp
- URLS = [...]
- if __name__ == "__main__":
- print(
- asyncio.get_event_loop().run_until_complete(
- asyncio.gather(*(foo(url) for url in URLS))))
- async def foo(url):
- async with aiohttp.ClientSession() as s:
- async with s.head(...) as r:
- return url, r.headers[...]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement