我尝试发出异步请求来获取URL(Windows 7上的Anaconda Python 3.6.3 64位)
import asyncio
from aiohttp import ClientSession
async def hello(url):
async with ClientSession() as session:
async with session.get(url) as response:
response = await response.read()
print(response)
loop = asyncio.get_event_loop()
loop.run_until_complete(hello("http://httpbin.org/headers"))
(来自this article的代码)
但是我收到以下错误:
Traceback (most recent call last):
File "<ipython-input-89-ff1fbb8cc713>", line 1, in <module>
runfile('C:/Users/me/Code/async_fetch.py', wdir='C:/Users/me/Code/')
File "C:\App\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
execfile(filename, namespace)
File "C:\App\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/me/Code/async_fetch.py", line 19, in <module>
loop.run_until_complete(hello("http://httpbin.org/headers"))
File "C:\APP\Anaconda3\envs\dashboards\lib\asyncio\base_events.py", line 467, in run_until_complete
return future.result()
File "C:/Users/me/Code/async_fetch.py", line 13, in hello
async with session.get(url) as response:
File "C:\App\Anaconda3\lib\site-packages\aiohttp\client.py", line 783, in __aenter__
self._resp = await self._coro
File "C:\App\Anaconda3\lib\site-packages\aiohttp\client.py", line 320, in _request
traces=traces
File "C:\App\Anaconda3\lib\site-packages\aiohttp\connector.py", line 418, in connect
traces=traces
File "C:\App\Anaconda3\lib\site-packages\aiohttp\connector.py", line 733, in _create_connection
traces=traces
File "C:\App\Anaconda3\lib\site-packages\aiohttp\connector.py", line 823, in _create_direct_connection
raise ClientConnectorError(req.connection_key, exc) from exc
ClientConnectorError: Cannot connect to host httpbin.org:80 ssl:False [getaddrinfo failed]