Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Python streaming output via paramiko ssh

I am trying to stream the output to my webapp of a python script (my_script.py) that is located on a remote host. On the remote host I can successfully run the python script like this and see the output:

# python3 /path/to/my_script.py

I am trying to do the same via parmiko's ssh and output the stream as it generates text but it seems to have to wait for client.exec_command to finish before it can get to the next line resulting in the whole entire text output and not line by line.

...

stdin, stdout, stderr = client.exec_command(
            'python3 /path/to/my_script.py')

print("Going into for loop now...")   # I don't see this print until the script above is complete 

output_string = ""
for line in io.TextIOWrapper(stdout):
    output_string += line
    logging.info(line)
    yield line

I know Python runs the code synchronously, however, is there a way to run client.exec_command and output to stdout at the same time?

Comments