TimeoutError:[Errno 60]连接呼叫失败('8.8.8.8',53)

时间:2018-11-17 01:30:34

标签: python-3.x python-asyncio

我正在用Python 3.7学习异步包。但是,当我尝试运行以下代码(来自http://markuseliasson.se/article/introduction-to-asyncio/)时,只能连接8.8.4.4,而不能连接8.8.8.8。

请问原因?谢谢!

代码如下:

import asyncio
from random import randint

index = 0

async def do_stuff(ip, port):

    global index
    index = index +1

    reader, writer = await asyncio.open_connection(ip,port)
    print('About to open a connection to {ip}'.format(ip=ip))

    num = randint(0,5)
    print(index,num)
    await asyncio.sleep(num)
    print('Connection open to {ip}'.format(ip=ip))

    writer.close()
    print('Closed connection to {ip}'.format(ip=ip))

if __name__ == '__main__':
    loop = asyncio.get_event_loop()

    work = [
        asyncio.ensure_future(do_stuff('8.8.8.8','53')),
        asyncio.ensure_future(do_stuff('8.8.8.8','53'))
    ]

    loop.run_until_complete(asyncio.gather(*work))

错误消息如下:

About to open a connection to 8.8.4.4
4
Connection open to 8.8.4.4
Closed connection to 8.8.4.4
Traceback (most recent call last):
  File "asyn.py", line 23, in <module>
    loop.run_until_complete(asyncio.gather(*work))
  File "/Users/Ten/anaconda3/envs/py37/lib/python3.7/asyncio/base_events.py", line 573, in run_until_complete
    return future.result()
  File "asyn.py", line 5, in do_stuff
    reader, writer = await asyncio.open_connection(ip,port)
  File "/Users/Ten/anaconda3/envs/py37/lib/python3.7/asyncio/streams.py", line 77, in open_connection
    lambda: protocol, host, port, **kwds)
  File "/Users/Ten/anaconda3/envs/py37/lib/python3.7/asyncio/base_events.py", line 948, in create_connection
    raise exceptions[0]
  File "/Users/Ten/anaconda3/envs/py37/lib/python3.7/asyncio/base_events.py", line 935, in create_connection
    await self.sock_connect(sock, address)
  File "/Users/Ten/anaconda3/envs/py37/lib/python3.7/asyncio/selector_events.py", line 475, in sock_connect
    return await fut
  File "/Users/Ten/anaconda3/envs/py37/lib/python3.7/asyncio/selector_events.py", line 505, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
TimeoutError: [Errno 60] Connect call failed ('8.8.8.8', 53)

0 个答案:

没有答案