0

I'm attempting to create a script in python that will upload a CSV to a vendors sftp server. After getting too any errors trying to use pysftp, I tried using Paramiko and got farther, but now I'm stuck at the actual upload part. When I attempt the upload it says

  File "C:\Users\me\PycharmProjects\pythonProject1\main.py", line 9, in <module>
    sftp.put(csv,dest)
  File "C:\Program Files\Python39\lib\site-packages\paramiko\sftp_client.py", line 759, in put
    return self.putfo(fl, remotepath, file_size, callback, confirm)
  File "C:\Program Files\Python39\lib\site-packages\paramiko\sftp_client.py", line 720, in putfo
    s = self.stat(remotepath)
  File "C:\Program Files\Python39\lib\site-packages\paramiko\sftp_client.py", line 493, in stat
    t, msg = self._request(CMD_STAT, path)
  File "C:\Program Files\Python39\lib\site-packages\paramiko\sftp_client.py", line 813, in _request
    return self._read_response(num)
  File "C:\Program Files\Python39\lib\site-packages\paramiko\sftp_client.py", line 865, in _read_response
    self._convert_status(msg)
  File "C:\Program Files\Python39\lib\site-packages\paramiko\sftp_client.py", line 894, in _convert_status
    raise IOError(errno.ENOENT, text)
FileNotFoundError: [Errno 2] No such file

Here's what I've come up with so far :

import paramiko
rsa_key = paramiko.RSAKey.from_private_key_file("C:/locationof/privkey", password='password')
csv = "C:/locationof/DELETE_users_snapshot.csv"
dest = "/upload/DELETE_users_snapshot.csv"
transport = paramiko.Transport(("host",4911))
transport.connect(username='user', pkey=rsa_key)
sftp = paramiko.SFTPClient.from_transport(transport)

sftp.put(csv,dest)

I can confirm that /upload/ does exist because I can connect via filezilla and see it/upload to it there just fine. Here is the filezilla log when uploading a file successfully :

Trace:  CControlSocket::SendNextCommand()
Trace:  CSftpConnectOpData::Send() in state 0
Status: Connecting to host:4911...
Trace:  Going to execute C:\Program Files\FileZilla FTP Client\fzsftp.exe
Response:   fzSftp started, protocol_version=10
Trace:  CSftpConnectOpData::ParseResponse() in state 0
Trace:  CControlSocket::SendNextCommand()
Trace:  CSftpConnectOpData::Send() in state 2
Command:    keyfile "C:\Users\me\Documents\privkey"
Trace:  CSftpConnectOpData::ParseResponse() in state 2
Trace:  CControlSocket::SendNextCommand()
Trace:  CSftpConnectOpData::Send() in state 3
Command:    open "user@host" 4911
Trace:  Looking up host "host" for SSH connection
Trace:  Connecting to host port 4911
Trace:  We claim version: SSH-2.0-FileZilla_3.52.0.1
Trace:  Remote version: SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.10
Trace:  Using SSH protocol version 2
Trace:  Doing ECDH key exchange with curve Curve25519 and hash SHA-256 (unaccelerated)
Trace:  Server also has ecdsa-sha2-nistp256/ssh-rsa host keys, but we don't know any of them
Trace:  Host key fingerprint is:
Trace:  ssh-ed25519 255 b6:bf:f6:7d:d5:ee:25:b9:b3:da:94:03:ce:16:bf:e6 iYh1W2ce0YiCyZo19PktVYQJwrYqjmn6qaopbW0HIzQ=
Trace:  Initialised AES-256 GCM outbound encryption
Trace:  Initialised AES256 GCM outbound MAC algorithm (in ETM mode) (required by cipher)
Trace:  Initialised AES-256 GCM inbound encryption
Trace:  Initialised AES256 GCM inbound MAC algorithm (in ETM mode) (required by cipher)
Trace:  Private key in 'C:\Users\me\Documents\privkey' is encrypted, defer loading until use.
Trace:  Successfully loaded 1 key pair from file
Status: Using username "user". 
Trace:  Offered public key from "C:\Users\me\Documents\privkey"
Trace:  Offer of public key accepted, trying to authenticate using it.
Trace:  CSftpControlSocket::SetAsyncRequestReply
Command:    Pass: ********
Trace:  Sent public key signature
Trace:  Access granted
Trace:  Opening main session channel
Trace:  Opened main channel
Trace:  Started a shell/command
Status: Connected to host
Trace:  Remote working directory is /upload
Trace:  CSftpConnectOpData::ParseResponse() in state 3
Trace:  CControlSocket::ResetOperation(0)
Trace:  CSftpConnectOpData::Reset(0) in state 3
Trace:  CControlSocket::SendNextCommand()
Trace:  CSftpListOpData::Send() in state 0
Status: Retrieving directory listing...
Trace:  CSftpChangeDirOpData::Send() in state 0
Trace:  CSftpChangeDirOpData::Send() in state 1
Command:    pwd
Response:   Current directory is: "/upload"
Trace:  CSftpChangeDirOpData::ParseResponse() in state 1
Trace:  CControlSocket::ResetOperation(0)
Trace:  CSftpChangeDirOpData::Reset(0) in state 1
Trace:  CSftpListOpData::SubcommandResult(0) in state 1
Trace:  CControlSocket::SendNextCommand()
Trace:  CSftpListOpData::Send() in state 2
Trace:  CSftpListOpData::Send() in state 3
Command:    ls
Status: Listing directory /upload
Trace:  CSftpListOpData::ParseResponse() in state 3
Trace:  CControlSocket::ResetOperation(0)
Trace:  CSftpListOpData::Reset(0) in state 3
Status: Directory listing of "/upload" successful
Trace:  CControlSocket::SendNextCommand()
Trace:  CSftpConnectOpData::Send() in state 0
Status: Connecting to host:4911...
Trace:  Going to execute C:\Program Files\FileZilla FTP Client\fzsftp.exe
Response:   fzSftp started, protocol_version=10
Trace:  CSftpConnectOpData::ParseResponse() in state 0
Trace:  CControlSocket::SendNextCommand()
Trace:  CSftpConnectOpData::Send() in state 2
Command:    keyfile "C:\Users\me\Documents\privkey"
Trace:  CSftpConnectOpData::ParseResponse() in state 2
Trace:  CControlSocket::SendNextCommand()
Trace:  CSftpConnectOpData::Send() in state 3
Command:    open "user@host" 4911
Trace:  Looking up host "host" for SSH connection
Trace:  Connecting to host port 4911
Trace:  We claim version: SSH-2.0-FileZilla_3.52.0.1
Trace:  Remote version: SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.10
Trace:  Using SSH protocol version 2
Trace:  Doing ECDH key exchange with curve Curve25519 and hash SHA-256 (unaccelerated)
Trace:  Server also has ecdsa-sha2-nistp256/ssh-rsa host keys, but we don't know any of them
Trace:  Host key fingerprint is:
Trace:  ssh-ed25519 255 b6:bf:f6:7d:d5:ee:25:b9:b3:da:94:03:ce:16:bf:e6 iYh1W2ce0YiCyZo19PktVYQJwrYqjmn6qaopbW0HIzQ=
Trace:  Initialised AES-256 GCM outbound encryption
Trace:  Initialised AES256 GCM outbound MAC algorithm (in ETM mode) (required by cipher)
Trace:  Initialised AES-256 GCM inbound encryption
Trace:  Initialised AES256 GCM inbound MAC algorithm (in ETM mode) (required by cipher)
Trace:  Private key in 'C:\Users\me\Documents\privkey' is encrypted, defer loading until use.
Trace:  Successfully loaded 1 key pair from file
Status: Using username "user". 
Trace:  Offered public key from "C:\Users\me\Documents\privkey"
Trace:  Offer of public key accepted, trying to authenticate using it.
Trace:  CSftpControlSocket::SetAsyncRequestReply
Command:    Pass: ********
Trace:  Sent public key signature
Trace:  Access granted
Trace:  Opening main session channel
Trace:  Opened main channel
Trace:  Started a shell/command
Status: Connected to host
Trace:  Remote working directory is /upload
Trace:  CSftpConnectOpData::ParseResponse() in state 3
Trace:  CControlSocket::ResetOperation(0)
Trace:  CSftpConnectOpData::Reset(0) in state 3
Trace:  CControlSocket::SendNextCommand()
Trace:  CSftpFileTransferOpData::Send() in state 0
Status: Starting upload of C:\Users\me\Documents\DELETE_snapshot.csv
Trace:  CSftpChangeDirOpData::Send() in state 0
Trace:  CSftpChangeDirOpData::Send() in state 2
Command:    cd "/upload"
Response:   New directory is: "/upload"
Trace:  CSftpChangeDirOpData::ParseResponse() in state 2
Trace:  CControlSocket::ResetOperation(0)
Trace:  CSftpChangeDirOpData::Reset(0) in state 2
Trace:  CSftpFileTransferOpData::SubcommandResult(0) in state 1
Trace:  CControlSocket::SendNextCommand()
Trace:  CSftpFileTransferOpData::Send() in state 4
Command:    put "C:\Users\me\Documents\DELETE_snapshot.csv" "DELETE_snapshot.csv"
Command:    local:C:\Users\me\Documents\DELETE_snapshot.csv => remote:/upload/DELETE_snapshot.csv
Trace:  CSftpFileTransferOpData::ParseResponse() in state 4
Trace:  CControlSocket::ResetOperation(0)
Trace:  CSftpFileTransferOpData::Reset(0) in state 4
Status: File transfer successful, transferred 75 bytes in 1 second
Trace:  CControlSocket::SendNextCommand()
Trace:  CSftpListOpData::Send() in state 0
Status: Retrieving directory listing of "/upload"...
Trace:  CSftpChangeDirOpData::Send() in state 0
Trace:  CControlSocket::ResetOperation(0)
Trace:  CSftpChangeDirOpData::Reset(0) in state 0
Trace:  CSftpListOpData::SubcommandResult(0) in state 1
Trace:  CControlSocket::SendNextCommand()
Trace:  CSftpListOpData::Send() in state 2
Trace:  CSftpListOpData::Send() in state 3
Command:    ls
Status: Listing directory /upload
Trace:  CSftpListOpData::ParseResponse() in state 3
Trace:  CControlSocket::ResetOperation(0)
Trace:  CSftpListOpData::Reset(0) in state 3
Status: Directory listing of "/upload" successful

Thanks in advance!

-Chester

  • Show us a complete exception callstack + Verbose FileZilla log file showing a successful upload of the same local file to the same remote directory. – Martin Prikryl Jan 21 '21 at 18:26
  • @MartinPrikryl I believe I've added what you're looking for. – Chester Bennett Jan 21 '21 at 22:29
  • FileZilla connects to port 4911, while your Python code connects to port 22. There are also other discrepancies in paths and usernames. While it's possible that those are because you have edited the code and logs before posting, at least make sure you edit them consistently. Otherwise it's difficult for us to help you. – Martin Prikryl Jan 22 '21 at 14:08
  • @MartinPrikryl Apologies. I was attempting to obscure the username and host and forgot to match all sets of logs/code. The user, host, and port should match in both the filezilla and python command now. – Chester Bennett Jan 22 '21 at 20:18
  • I guess you have this problem: [Paramiko put method throws “[Errno 2\] File not found” if SFTP server has trigger to automatically move file upon upload](https://stackoverflow.com/q/59621421/850848). – Martin Prikryl Jan 23 '21 at 07:15

0 Answers0