1

I am trying to put together an SFTP module for use with a drone in a project for school. Security is not a concern for me right now, as I am simply trying to get successful connections and file transfers established first.

import paramiko

#Open Transport
host, port = "IP Address", 22
transport = paramiko.Transport((host,port))

#Authentication
username, password = "Username", "Password"
transport.connect(None, username, password)

#Connect
sftp = paramiko.SFTPClient.from_transport(transport)

#Download
remotePath = "C:\\Users\\User\\Desktop\\Upload Data\\Test.txt"
localPath = "C:\\Users\\User\\Downloads\\Test.txt"
sftp.get(remotePath, localPath)

#Upload
remotePath = "C:\\Users\\User\\Desktop\\Upload Data"
localPath = "C:\\Users\\User\\Downloads\\Test.txt"
sftp.put(localPath, remotePath)

#Close
if sftp: sftp.close()
if transport: transport.close()

When running the above code, I get FileNotFound: [Errno 2] No such file returned. The same error is returned for attempted uploads. I am hosting the SFTP server on a Windows platform using OpenSSH, as the paths may suggest.

The sshd_config file is pointing correctly to the path defined in remotePath. Firewall rules on the server are correctly configured, as I am able to connect to the SFTP server using FileZilla from the same computer I am attempting to connect to the server from using Python. I can ping the remote server using the same machine as well. As far as I can tell, this is not a connectivity issue between the server and client.

I'm really not sure what I am doing wrong. I have quadruple checked the paths in my code are correct. I have been knee-deep in tabs for the past couple of hours trying to troubleshoot, but I'm running out of ideas and so I need to ask the internet.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Selovanth
  • 21
  • 1

1 Answers1

0

The C:\Users\User\Desktop\Upload Data\Test.txt is not what a typical SFTP path looks like. It rather looks like a physical path on the server.

Login with GUI SFTP client and see what is the actual SFTP path to your files.

If could be like (or any other):

  • /Upload Data/Test.txt
  • /C/Users/User/Desktop/Upload Data/Test.txt
  • /Test.txt

See also SFTP path format versus local path format.


Obligatory warning: Do not use Transport class directly this way – You are bypassing a host key check and consequently losing a protection against MITM attacks. Instead use SSHClient class. See also Paramiko "Unknown Server".

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992