Reputation: 31
#!/usr/bin/env python3.5
import asyncio
import aiohttp
url = "http://eniig.dk"
async def main():
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
try:
body = await response.read()
print(response.method)
print(body)
except UnicodeDecodeError as msg:
print(msg)
print(body)
except Exception as msg:
print(msg)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
This code gives an error:
400, message='400, message='invalid constant string''
If the URL specifies 'https', then everything is OK. But how to avoid an error if the protocol is not known in advance?
What alternatives will advise aiohttp?
UPD: I have not been here for a long time. Forgot to say that the code was run under Debian 8 stable. The problem was solved by replacing the methods ClientSession() and get() by TCPConnector() and request()
Upvotes: 3
Views: 1347
Reputation: 9826
It's looks like a pretty good code.
Worked for me with this versions of aiohttp
:
Upvotes: 2