How to use paramiko to talk interactivity with a remote application?
Question: I am trying to use paramiko to interatively speak with an command line
application, but I'm doing something wrong.

    
    
    # that's the echo.py, the script I am connecting to via SSH
    import sys, time
    while 1:
        x = sys.stdin.readline()
        sys.stdout.write("got-" + x) # x already contains newline
    
    
    
    # client.py
    import paramiko
    
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    ssh.connect('127.0.0.1', username='user', password='pass')
    
    (stdin, stdout, stderr) = ssh.exec_command(r"python C:\test\echo.py")
    
    stdin.write("xxx\n")
    print "got back [%s]" % stdout.read()  # <<< here the code got stuck, getting nothing back
    

Note: the only case that worked for me was to have the `echo.py` quit and do
`stdout.readlines()` on the client, but obviously this is not what I was
looking for.

I need to be able to **send and receive messages by using stdin and stdout** ,
optionally with some kind of timeout support.


Answer: I believe the problem is that when you run `sys.stdout.write` it writes to the
STDOUT buffer, but the buffer is not flushed until done so manually or closed
(stdout is a [File Object](http://docs.python.org/library/stdtypes.html#file-
objects), which you can confirm yourself with `type(sys.stdout)`). Since your
loop is infinite, the buffer is never flushed.

Changing your loop in echo.py to this should do the trick:

    
    
    while 1:
        x = sys.stdin.readline()
        sys.stdout.write("got-" + x) # x already contains newline
        sys.stdout.flush() # flush the buffer
    

