linux - Line-by-line Remote Data Transmission in Python -


I'm playing with the subprocess module so that each row can be sent to the input file, the following commands The process created by

  ssh -t -a $ host 'remote_command'  

remote_index line in STDIN, on some processing line and The std revises the cycle until STDIN closes or reaches the EOF.

To achieve this, what I was doing was:

  process = subprances. For line in popin ("/ tmp / foo") ("SST-T-A $ host 'remote_command' ', shell = tru, stdin = subprosec .ppe): process.stdin.write (line) Process.stdin. Flush () process.stdin.close ()  

But I learned that the method above is not strong enough, because it often happens that remote contract Completes the entire content prematurely without processing (though sometimes the same code succeeds successfully)

The situation is same When I work with each other, however, very similar, approach:

  process = subprocess.Popen ("SST -T-A $ Host 'remote_command' ', shell = true So the question is:  How can I make sure that each row in an input file, remote machine in Python is stdin = file ('/ tmp / foo'))  

By the end, sent, received, and processed?

If this ...

  process = sub-active D. ('/ Tmp / foo') in the file for the line ("ssh -tA $ host 'remote_command' ', shell = true, stdin = subprocess.PIPE): process.stdin.write (line) process.stdin.flush () Process.stdin.close ()  

... Your entire program (not necessary) will not work.

However the last call to process.stdin.close () will ensure that all data has been sent to the ssh process before the end of your program O, it will not ensure that the ssh process has sent all the data over the entire network, so there may be some excellent data for it to send it.

Unfortunately, because the ssh process is a child process in your program, then when your program is finished, the ssh process will be received < Code> SIGHUP which will immediately kill it, possibly before it sends all its data.

Until the end of the remote_command is terminated, it is not a problem, and you can not process ssh to SIGHUP can be ignored, and keep it running in the background ...

  process = subprances POPEEN ("NOHOP SST-T-A $ host 'remote_command'" ... ...)  

... or process your program to Ssh By ending, adding ...

  process.wait ()  

... at the end of your program.


Update

On the next exam, it appears that a process gets only SIGHUP , If its controller TTI does not end its original process.

This may be something with the -t option which creates a new controlled TTI on the remote host, and this subprocess spawns have expired.

In which case, you may need it ...

  process = subprocess. Try it without the popen ("ssh-t-A $ host 'nohup remote_command'", ...)  

... or without the -t option.


Comments