如何通过线程概念将CTRL + C传递给python paramiko?

时间:2017-08-12 08:44:38

标签: multithreading python-2.7 ssh paramiko python-multithreading

我已经执行了一段时间(定时活动)的python-paramiko片段,然后在一定的固定时间后发送 CTRL + C (继续为此写一些数据)固定的时间)。没错,这绝对没问题。

现在,我想以更好的方式做同样的事情。比如说,让它继续写入数据,直到活动(未定时)完成。活动完成后,我应该停止该脚本或停止写入数据&关闭连接。我该怎么做 ?我需要使用线程吗?你能救我吗?

import paramiko

def fun1():
    try:
            ssh = paramiko.SSHClient()
            ssh.load_system_host_keys()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(ip,username=username,password=password)
        except Exception as err:
            print err

阅读:

while True: #while time.time() - start < 3:
    if session.recv_ready():
        data = session.recv(1024)
        sys.stdout.write(data)
        sys.stdout.flush() # Flushing is important!
    time.sleep(0.001) # Yield CPU so we don't take up 100% usage...
sys.stdout.write('\n')

0 个答案:

没有答案