Python脚本引发错误并退出,出现OSError:[Errno 113] portcan上的Asyncio连接调用失败

时间:2019-05-14 01:06:21

标签: python python-asyncio

在gist.github上找到了这段有趣的代码: https://gist.github.com/emillynge/1eee20e8429caece09774af7e5614d69

如果远程网络的响应速度足够快(默认值为“ timeout = 0.1”),则效果很好。但是,在实际的实时应用中,通常需要提高这一点。特别是,在扫描网络范围时,此脚本几乎每次都会发出错误,超时低至0.2。

看代码,似乎脚本应该忽略这些错误,但是实际上,它立即退出了工作。

     except (asyncio.TimeoutError, ConnectionRefusedError):
                pass
            else:
                yield task  # yield successful tasks

这是我得到的全部错误:

unhandled exception during asyncio.run() shutdown
task: <Task finished coro=<<async_generator_athrow without __name__>()> exception=RuntimeError("can't send non-None value to a just-started coroutine")>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "port.py", line 130, in main
    async for task in streamer:
  File "/usr/local/lib/python3.7/site-packages/aiostream/stream/advanced.py", line 43, in base_combine
    result = getter()
  File "port.py", line 79, in task_worker
    await asyncio.wait_for(conn, task.timeout)
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 416, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/asyncio/streams.py", line 77, in open_connection
    lambda: protocol, host, port, **kwds)
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 959, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 946, in create_connection
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.7/asyncio/selector_events.py", line 464, in sock_connect
    return await fut
  File "/usr/local/lib/python3.7/asyncio/selector_events.py", line 494, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
OSError: [Errno 113] Connect call failed ('184.183.182.181', 8001)

During handling of the above exception, another exception occurred:

RuntimeError: can't send non-None value to a just-started coroutine
Traceback (most recent call last):
  File "port.py", line 153, in <module>
    asyncio.run(main(sys.argv[1], ''.join(sys.argv[2:])))
  File "/usr/local/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "port.py", line 130, in main
    async for task in streamer:
  File "/usr/local/lib/python3.7/site-packages/aiostream/stream/advanced.py", line 43, in base_combine
    result = getter()
  File "port.py", line 79, in task_worker
    await asyncio.wait_for(conn, task.timeout)
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 416, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/asyncio/streams.py", line 77, in open_connection
    lambda: protocol, host, port, **kwds)
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 959, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 946, in create_connection
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.7/asyncio/selector_events.py", line 464, in sock_connect
    return await fut
  File "/usr/local/lib/python3.7/asyncio/selector_events.py", line 494, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
OSError: [Errno 113] Connect call failed ('184.183.182.181', 8001)

目标是忽略错误并完成循环。 任何输入表示赞赏。

0 个答案:

没有答案