@@ -285,8 +285,10 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
285285mode = "r+b" if binary else "r+"
286286
287287with tempfile .NamedTemporaryFile (mode = mode ,delete = False )as tmp_file :
288+ scp_ssh_cmd = ['-P' if x == '-p' else x for x in self .ssh_cmd ]
289+
288290if not truncate :
289- scp_cmd = ['scp' ]+ self . ssh_cmd + [f"{ self .username } @{ self .host } :{ filename } " ,tmp_file .name ]
291+ scp_cmd = ['scp' ]+ scp_ssh_cmd + [f"{ self .username } @{ self .host } :{ filename } " ,tmp_file .name ]
290292subprocess .run (scp_cmd ,check = False )# The file might not exist yet
291293tmp_file .seek (0 ,os .SEEK_END )
292294
@@ -302,11 +304,12 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
302304tmp_file .write (data )
303305
304306tmp_file .flush ()
305- scp_cmd = ['scp' ]+ self .ssh_cmd + [tmp_file .name ,f"{ self .username } @{ self .host } :{ filename } " ]
307+ # Because in scp we set up port using -P option
308+ scp_cmd = ['scp' ]+ scp_ssh_cmd + [tmp_file .name ,f"{ self .username } @{ self .host } :{ filename } " ]
306309subprocess .run (scp_cmd ,check = True )
307310
308311remote_directory = os .path .dirname (filename )
309- mkdir_cmd = ['ssh' ]+ self . ssh_cmd + [f"{ self .username } @{ self .host } " ,f"mkdir -p{ remote_directory } " ]
312+ mkdir_cmd = ['ssh' ]+ scp_ssh_cmd + [f"{ self .username } @{ self .host } " ,f"mkdir -p{ remote_directory } " ]
310313subprocess .run (mkdir_cmd ,check = True )
311314
312315os .remove (tmp_file .name )