Update: it looks like the issue is fixed in the github version (0.7.2). It doesn't produce the error. As @danj.py said, it is fixed by "Get rid of __del__
in connector" commit.
It is not ProactorEventLoop or Windows specific. I can reproduce the error on Ubuntu with the default event loop:
#!/usr/bin/env python3
import asyncio
import aiohttp # $ pip install aiohttp
@asyncio.coroutine
def get_body(url):
response = yield from aiohttp.request('GET', url)
return (yield from response.read_and_close())
loop = asyncio.get_event_loop()
body = loop.run_until_complete(get_body('https://stackoverflow.com/q/23283502'))
print(len(body), type(body), body[:200])
loop.close()
It might be a bug in aiohttp because the usage seems correct.
There is no error if the request is made without aiohttp
:
#!/usr/bin/env python3
import asyncio
from contextlib import closing
from urllib.parse import urlsplit
@asyncio.coroutine
def get_body(url):
# parse url
url = urlsplit(url)
path = '/' * (not url.path) + url.path + '?' * bool(url.query) + url.query
# open connection
reader, writer = yield from asyncio.open_connection(
host=url.hostname,
port=url.port or (443 if url.scheme == 'https' else 80),
ssl=(url.scheme == 'https'))
with closing(writer):
# send request
writer.write(b'GET ' + path.encode('ascii') + b' HTTP/1.1\r\n'
b'Host: ' + url.netloc.encode('ascii') + b'\r\n'
b'Connection: close\r\n\r\n')
# read headers
while True:
line = yield from reader.readline()
line = line.rstrip(b'\n\r')
print(line.decode('latin-1'))
if not line:
break
# read body
body = yield from reader.read()
return body
loop = asyncio.get_event_loop()
body = loop.run_until_complete(get_body('https://stackoverflow.com/q/23283502'))
print(len(body), type(body), body[:200])
loop.close()
Note: the examples are not completely equivalent e.g., the latter doesn't follow redirects.