I have a script that writes a file to disk and then needs to upload it to a remote server using psftp.
based on some of answers I have seen on this site I have a basic idea of what I want to do but because:
My code is written in Python 3.x
My code uses a mix of strings and variables in the commands
I have not been successful.
here is my code ( after setting up the variables)
connection_string = "" +prog+ "-P " +port+ "-pw " +passwd+ " " +user+"@"+host
p = subprocess.Popen(connection_string.split(),stdin=subprocess.PIPE, stdout=subprocess.PIPE,shell=True)
sftp_cmd = "put "+upload_file
encoded_cmd = str.encode(sftp_cmd)
p.stdin.write(encoded_cmd)
# I would have rather used p.stdin.write(b'sftp_cmd') to create a buffer from the string but that didn't seem to work either.
the code compiles but the file does not get uploaded
1) What am I doing wrong ? :-)
2) How can I see if a connection is made and what command actually goes through ?
3) Is this the best way to call psftp or perhapes I should write a batch file on the fly each time and use the "-b" flag to call it ?
Note. I cannot use any libraries that do not come with the standard Python distro in my code. So I have to use psftp.
referenced answers:
Can i control PSFTP from a Python script?
formatting strings for stdin.write() in python 3.x
TIA