Real-time reading of terminal output from server

雨燕双飞 提交于 2019-12-12 01:24:43

问题


I'm trying to process images from my camera on my server and get the information after processing on my local machine in real-time. I can get necessary information as terminal outputs on my server, but I can't put this info in my python code on local machine, until my server program is running. I'm tried this code:

cmd="sshpass -p 'pass' ssh -Y user@ip -t 'process_image; bash -l'"
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1)
for line in iter(p.stdout.readline, b''):
    print(line)
    p.stdout.close()
    p.wait()

But it doesn't work - it looks like this code just paused my program. I tried to write output to file and than read file from local machine, but it distorts my data. What can i do to read terminal output from server in real-time?


回答1:


Since the output is going to be line buffered since you use bufsize=1, then you could just do:

cmd="sshpass -p 'pass' ssh -Y user@ip -t 'process_image; bash -l'"
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1)
for line in p.stdout:
    print(line)
    .....

Of course, this assumes your command is giving you the output you expect.



来源:https://stackoverflow.com/questions/41908888/real-time-reading-of-terminal-output-from-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!