小编Dmi*_*rev的帖子

paramiko python模块挂在stdout.read()

我使用以下代码:

import paramiko

def runSshCmd(hostname, username, password, cmd, timeout=None):          
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(hostname, username=username, password=password,
            allow_agent=False, look_for_keys=False, timeout=timeout) 
    stdin, stdout, stderr = client.exec_command(cmd)
    stdin.flush()
    data = stdout.read()
    print (data)
    client.close()

runSshCmd("10.128.12.32", "root", "C0mput3Gr!d", "ts_menu")
Run Code Online (Sandbox Code Playgroud)

当谈到stdout.read()时,它会挂起......有时它会在很长时间后打印输出.

你能否建议如果能解决这个问题?

我看到这个问题已经报道:

https://bugs.python.org/issue24026

在python中是否有更好的模块用于ssh连接和运行命令?

python python-module paramiko python-3.x

10
推荐指数
2
解决办法
8356
查看次数

Python 使用 pytest 模拟修补另一个函数中的协程函数?

我在 module_name/app.py 中有两个函数

async def f1(arg):
    # do something
    return arg + '1'


async def f2(arg):
    result = await f1(arg)
    return result
Run Code Online (Sandbox Code Playgroud)

我尝试使用 pytest 和 asynctest 测试 f2 和模拟 f1。
只有我这样做才有效

def sf_f1(arg):
    return 'some value'

@pytest.mark.asyncio
async def test_f2():
    with asynctest.mock.patch('module_name.app.f1', side_effect=sf_f1):
        assert 'some value' == await f2('test')
Run Code Online (Sandbox Code Playgroud)

考试通过了

但是,我想做这样的事情

import module_name

@pytest.fixture()
def mock_f1():
    return asynctest.CoroutineMock(module_name.app.f1, side_effect=sf_f1)


@pytest.mark.asyncio
async def test_f2_2(mock_f1):
    assert 'some value' == await f2('test')
Run Code Online (Sandbox Code Playgroud)

我明白了

   assert 'some value' == await f2('test')
   AssertionError: assert 'some value' …
Run Code Online (Sandbox Code Playgroud)

python pytest python-asyncio

5
推荐指数
1
解决办法
4541
查看次数