Python OSError: Failure with SFTP

Question:

I’m testing SFTP communication on a Windows 11 laptop with SFTP server running at localhost:3373. An sftp.get request generates an "OSError: Failure" error with this code:

import pysftp

remotepath = "C:/Users/Profile/sftpdata/remote/gimme.txt"
localpath = "C:/Users/Profile/sftpdata/local/gimme.txt"

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection('localhost', port=3373, username='admin', password='admin', cnopts=cnopts) as sftp:
    sftp.get(remotepath, localpath=localpath)

The traceback:

Traceback (most recent call last):
  File "C:UsersProfilesftpsrctest_sftp.py", line 9, in <module>
    sftp.get(remotepath, localpath=localpath)
  File "C:UsersProfileAppDataLocalProgramsPythonPython310libsite-packagespysftp__init__.py", line 249, in get
    self._sftp.get(remotepath, localpath, callback=callback)
  File "C:UsersProfileAppDataLocalProgramsPythonPython310libsite-packagesparamikosftp_client.py", line 811, in get
    size = self.getfo(remotepath, fl, callback, prefetch)
  File "C:UsersProfileAppDataLocalProgramsPythonPython310libsite-packagesparamikosftp_client.py", line 782, in getfo
    file_size = self.stat(remotepath).st_size
  File "C:UsersProfileAppDataLocalProgramsPythonPython310libsite-packagesparamikosftp_client.py", line 493, in stat
    t, msg = self._request(CMD_STAT, path)
  File "C:UsersProfileAppDataLocalProgramsPythonPython310libsite-packagesparamikosftp_client.py", line 822, in _request
    return self._read_response(num)
  File "C:UsersProfileAppDataLocalProgramsPythonPython310libsite-packagesparamikosftp_client.py", line 874, in _read_response
    self._convert_status(msg)
  File "C:UsersProfileAppDataLocalProgramsPythonPython310libsite-packagesparamikosftp_client.py", line 907, in _convert_status
    raise IOError(text)
OSError: Failure

The environment is Windows 11, Python, Paramiko 3.0.0, sftpserver and pysftp.

The file gimme.txt is definitely in the remote folder. Have tried transforming the path statements using Path + as_posix() and realpath but with no luck. The generated key is rsa-ssh 4096.

Btw, localpath = "C:/Users/Profile/sftpdata/local" gives a Permission Error.

What am I doing wrong?

Asked By: Henry Thornton

||

Answers:

The C:/Users/Profile/sftpdata/remote/gimme.txt is not what a typical SFTP path looks like. It rather looks like a physical path on the server.

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

It could be like (or any other):

  • /sftpdata/remote/gimme.txt
  • /C/Users/Profile/sftpdata/remote/gimme.txt
  • /gimme.txt

See also SFTP path format versus local path format.


Side note: you should not use pysftp. It’s a dead project. Use Paramiko. See pysftp vs. Paramiko.

If you want to keep using pysftp, at least do not set cnopts.hostkeys = None, unless you do not care about security. For the correct solution see Verify host key with pysftp.

Answered By: Martin Prikryl
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.