asyncssh-如何在仍然获得用户输入的同时创建侦听器

时间:2018-12-31 17:48:35

标签: python python-asyncio

我正在使用网络设备,我编写了一个使用asyncssh对网络设备执行ssh的程序。

网络设备有时会通过会话使用广播发送更新。 我想做的是使一个侦听器能够侦听stdout,并且仍然可以获取用户输入(可能是某种形式的rest api函数)。

有可能这样做吗?

1 个答案:

答案 0 :(得分:1)

此堆栈溢出答案显示了如何收听stdin异步:

https://stackoverflow.com/a/35514777/10840818

编写一些代码。你想做什么?以下是asyncSSH文档中的示例。您是否有关于asyncSSH的问题?

import asyncio, asyncssh, sys

async def run_client():
    async with asyncssh.connect('localhost') as conn:
        result = await conn.run('echo "Hello!"', check=True)
        print(result.stdout, end='')

try:
    asyncio.get_event_loop().run_until_complete(run_client())
except (OSError, asyncssh.Error) as exc:
    sys.exit('SSH connection failed: ' + str(exc))

如果您需要服务器进程,请查看sanic并设置一个rest API而不是stdin进行通信。