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:\Users\Profile\sftpsrc\test_sftp.py", line 9, in <module>
sftp.get(remotepath, localpath=localpath)
File "C:\Users\Profile\AppData\Local\Programs\Python\Python310\lib\site-packages\pysftp\__init__.py", line 249, in get
self._sftp.get(remotepath, localpath, callback=callback)
File "C:\Users\Profile\AppData\Local\Programs\Python\Python310\lib\site-packages\paramiko\sftp_client.py", line 811, in get
size = self.getfo(remotepath, fl, callback, prefetch)
File "C:\Users\Profile\AppData\Local\Programs\Python\Python310\lib\site-packages\paramiko\sftp_client.py", line 782, in getfo
file_size = self.stat(remotepath).st_size
File "C:\Users\Profile\AppData\Local\Programs\Python\Python310\lib\site-packages\paramiko\sftp_client.py", line 493, in stat
t, msg = self._request(CMD_STAT, path)
File "C:\Users\Profile\AppData\Local\Programs\Python\Python310\lib\site-packages\paramiko\sftp_client.py", line 822, in _request
return self._read_response(num)
File "C:\Users\Profile\AppData\Local\Programs\Python\Python310\lib\site-packages\paramiko\sftp_client.py", line 874, in _read_response
self._convert_status(msg)
File "C:\Users\Profile\AppData\Local\Programs\Python\Python310\lib\site-packages\paramiko\sftp_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?
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.