Support the GITP forums on Patreon
Help support GITP's forums (and ongoing server maintenance) via Patreon
Results 1 to 7 of 7
  1. - Top - End - #1
    Ettin in the Playground
     
    BardGuy

    Join Date
    Jan 2009

    Default Python SFTP help

    Note: a few months ago I asked about something similar, but it was for FTP.

    I've now got to programmatically upload files to an SFTP (really SFTP this time) site, using Python.

    I found useful advice at
    https://stackoverflow.com/questions/...rm-independent
    but I'm having some issues and having trouble following.

    Passwords, username, and host are changed in the below.

    Code:
    host='sftp.cloud.something.com'
    user='myusername'
    pw='mypassword
    
    filepath="\\\s1prod\\office\\myfolder"
    filename=filepath+'\manifest.csv'
    
    import paramiko
    
    port = 22
    transport = paramiko.Transport((host, port))
    
    transport.connect(username = user, password = pw)
    
    sftp = paramiko.SFTPClient.from_transport(transport)
    
    sftp.put(filename,'manifest.csv')
    sftp.close()
    transport.close()
    I (think I) get an error at the line
    sftp.put(filename,'manifest.csv')
    so I think I am able to log into the SFTP site correctly, but I don't really understand PUT statements. (I have logged in via WinSCP successfully--but I want to to do this programmatically since I have to upload files to several different SFTP accounts.)

    Here's the error log, taking out my name from the C: path.
    Code:
    Traceback (most recent call last):
    
      File "<ipython-input-13-7f70cba9d8eb>", line 10, in <module>
        sftp.put(filename,'manifest.csv')
    
      File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\paramiko\sftp_client.py", line 759, in put
        return self.putfo(fl, remotepath, file_size, callback, confirm)
    
      File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\paramiko\sftp_client.py", line 714, in putfo
        with self.file(remotepath, "wb") as fr:
    
      File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\paramiko\sftp_client.py", line 372, in open
        t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
    
      File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\paramiko\sftp_client.py", line 813, in _request
        return self._read_response(num)
    
      File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\paramiko\sftp_client.py", line 865, in _read_response
        self._convert_status(msg)
    
      File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\paramiko\sftp_client.py", line 896, in _convert_status
        raise IOError(errno.EACCES, text)
    
    PermissionError: [Errno 13] Permission denied
    Spoiler: one issue I know isn't the case
    Show

    I was worried maybe my syntax is wrong for the program to find the CSV that I want to put on the SFTP site.

    But this code worked fine:
    Code:
    #testing that Python can find the file--it can!
    import pandas
    mydata=pandas.read_csv(filename)
    print(mydata)
    So I think the filename variable is working correctly.
    Last edited by JeenLeen; 2021-02-11 at 02:50 PM.

  2. - Top - End - #2
    Troll in the Playground
     
    DwarfClericGuy

    Join Date
    Jun 2007

    Default Re: Python SFTP help

    It's a put error, yes.

    PermissionError: [Errno 13] Permission denied

    As for something to try:

    filepath='\\\\s1prod\\office\\myfolder'
    filename=filepath+ '\\manifest.csv'
    May you get EXACTLY what you wish for.

  3. - Top - End - #3
    Bugbear in the Playground
     
    Whoracle's Avatar

    Join Date
    Jul 2004
    Location
    Freiburg, germany
    Gender
    Male

    Default Re: Python SFTP help

    a) Nitpick: filename=filepath+'\manifest.csv' should be filename=filepath+'\\manifest.csv' - shouldn't change your error, though
    b) you're correct, you're getting the error on put() - specifically, the other end is telling you that wou don't have write permissions on the target path. Check the permissions on the SFTP end for your user. Try it with Filezilla to make sure it's a code problem and not a receiver problem, maybe?

  4. - Top - End - #4
    Ettin in the Playground
     
    BardGuy

    Join Date
    Jan 2009

    Default Re: Python SFTP help

    Quote Originally Posted by Whoracle View Post
    a) Nitpick: filename=filepath+'\manifest.csv' should be filename=filepath+'\\manifest.csv' - shouldn't change your error, though
    Upon testing in WinSCP, I learnt that I don't have access to the root directory but do have one to an "uploads" folder. I wish the person had told me that.

    I fixed a couple things, and it's no longer giving me an error, but neither is a file showing up in the SFTP site. Here's the revised snippet of code
    Code:
    filename=filepath+'\\manifest.csv'
    
    #testing that Python can find the file--it can!
    #import pandas
    #mydata=pandas.read_csv(filename)
    #print(mydata)
    
    import paramiko
    
    port = 22
    transport = paramiko.Transport((host, port))
    
    transport.connect(username = user, password = pw)
    
    sftp = paramiko.SFTPClient.from_transport(transport)
    sftp.put(filename,'./uploads/manifest.csv')
    sftp.close()
    transport.close()
    Any suggestions about editing this line to upload the file to the "uploads" folder on the SFTP site?
    sftp.put(filename,'./uploads/manifest.csv')




    EDIT
    I've been trying various forward- and backslashes in the name.
    If I try
    Code:
    sftp.put(filename,'.\\uploads\\manifest.csv')
    I get this error
    Code:
      File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\paramiko\sftp_client.py", line 896, in _convert_status
        raise IOError(errno.EACCES, text)
    
    PermissionError: [Errno 13] Permission denied
    But I'm able to put files there via WinSCP...


    b) you're correct, you're getting the error on put() - specifically, the other end is telling you that wou don't have write permissions on the target path. Check the permissions on the SFTP end for your user. Try it with Filezilla to make sure it's a code problem and not a receiver problem, maybe?
    Unfortunately, I only have my work computer, and I can't download new software on it. So I can't check via Filezilla. But it is working via WinSCP login.
    Is there a way to check the SFTP permissions beyond just asking them "Hey, is there something blocking Python code from sending you files?"
    Last edited by JeenLeen; 2021-02-11 at 03:27 PM.

  5. - Top - End - #5
    Troll in the Playground
     
    DwarfClericGuy

    Join Date
    Jun 2007

    Default Re: Python SFTP help

    Try getting with the other side.

    Since it's an CSV file, I'm going to assume you're uploading to some sort of service that processes a standardized CSV and stores information for reporting and analysis.

    Some of those services won't let you list the directory, or, upon upload, have it almost immediately move to a 'processing' folder that's not visible to you.

    And try sftp.put(filename,'./uploads/manifest.csv')
    Last edited by sihnfahl; 2021-02-11 at 03:30 PM.
    May you get EXACTLY what you wish for.

  6. - Top - End - #6
    Ettin in the Playground
     
    BardGuy

    Join Date
    Jan 2009

    Default Re: Python SFTP help

    Quote Originally Posted by sihnfahl View Post
    Try getting with the other side.

    Since it's an CSV file, I'm going to assume you're uploading to some sort of service that processes a standardized CSV and stores information for reporting and analysis.

    Some of those services won't let you list the directory, or, upon upload, have it almost immediately move to a 'processing' folder that's not visible to you.

    And try sftp.put(filename,'./uploads/manifest.csv')
    I uploaded a file via WinSCP, exited WinSCP, and re-entered the SFTP site. The file is gone. So maybe that is what happened. I e-mailed my contact to ask him if the file is there or not.
    If it is there, I think my program is working now. I just can't confirm it on my end, which is frustrating. But, hey, at least it seems to be working.

    Many thanks, and hopefully my next post will be saying "yep, it's good" and not a follow-up question!

  7. - Top - End - #7
    Bugbear in the Playground
     
    Whoracle's Avatar

    Join Date
    Jul 2004
    Location
    Freiburg, germany
    Gender
    Male

    Default Re: Python SFTP help

    Quote Originally Posted by JeenLeen View Post
    I've been trying various forward- and backslashes in the name.
    For all things files and paths you should really give pathlib a look, especially under windows or when writing something that's supposed to be portable.

    Unfortunately, I only have my work computer, and I can't download new software on it. So I can't check via Filezilla. But it is working via WinSCP login.
    Is there a way to check the SFTP permissions beyond just asking them "Hey, is there something blocking Python code from sending you files?"
    Yeah, I went with FileZilla since that's the more known, but WinSCP is fine, too. IIRC you should be able to right-click the "." (current path) symbol in the remote file pane and select "show permissions" or "properties" or somesuch. Alternatively, paramiko should be able to do something like

    Code:
    username ="youruser"
    password = "yourpass"
    host = "sftphost"
    port = int("sftpport")
    
    ssh = paramiko.SSHClient()
    ssh.connect(host, port, username, password, look_for_keys=False)
    stdin, stdout, stderr = ssh.exec_command("ls -la")
    print(stdout.readlines())
    That's with the SSH functionality of Paramiko, may be the same with Transport(), dunno. That should list you the Posix permissions on the remote end.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •