Thread: ftplib

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13

    ftplib


    I'm not sure if this is a server side issue or what but I can't seem to upload a file to a server. For example:

    Code:
    from ftplib import FTP
    
    username = 'Shiner_Man'
    password = 'password'
    file = 'C:\test.txt'
    
    try:
        ftp = FTP('www.someftpserver.com')
    except:
        print "Server down?  I can't connect."
    
    try:
        ftp.login(username, password)
    except:
        print "Username or password is wrong dude."
    
    ftp.storbinary('STOR ' + file, open(file, 'rb'), 1024)
    which gives this:

    Code:
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
      File "C:\Python24\lib\ftplib.py", line 415, in storbinary
        conn = self.transfercmd(cmd)
      File "C:\Python24\lib\ftplib.py", line 345, in transfercmd
        return self.ntransfercmd(cmd, rest)[0]
      File "C:\Python24\lib\ftplib.py", line 334, in ntransfercmd
        resp = self.sendcmd(cmd)
      File "C:\Python24\lib\ftplib.py", line 241, in sendcmd
        return self.getresp()
      File "C:\Python24\lib\ftplib.py", line 214, in getresp
        raise error_perm, resp
    error_perm: 550 Cannot STOR.
    I'm just following a few examples I found but I can't get this to work correctly. Any ideas?
    Last edited by Shiner_Man; June 26th, 2005 at 10:56 PM.
    I'll learn this stuff someday.
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    This does not look like a Python error - it is the protocol engine reporting you cant write a file. Can you use another FTP client with the same info?

    Do you have an account that is recognised by the FTP server?
    Just having a *nix account is not always enough.

    Grim
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    I can upload to the server if I manually log in. The username and password is correct and I do have upload privelages.

    From the command line, I have to use passive ftp to do anything. Now I don't know if this is the issue. Maybe it is...?
    I'll learn this stuff someday.
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    According to the module docs passive mode "on" is the default state for Python > 2.0. It could be worth experimenting - using set_pasv(True) before opening the FTP connection.

    Iv'e not seen this problem myself. What version of Python are you using?

    grim
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    I'm using Python 2.4. I did try to modify the code as such:

    Code:
    from ftplib import FTP
    
    username = 'Shiner_Man'
    password = 'password'
    file = 'C:\test.txt'
    
    try:
        ftp = FTP('www.someftpserver.com')
    except:
        print "Server down?  I can't connect."
    
    ftp.set_pasv(1)
    
    try:
        ftp.login(username, password)
    except:
        print "Username or password is wrong dude."
    
    ftp.storbinary('STOR ' + file, open(file, 'rb'), 1024)
    This isn't working either. I think this may be on the server side although, as I said, I am able to upload/download files from the command line.
    I'll learn this stuff someday.
  10. #6
  11. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    grim
    Last edited by Grim Archon; June 27th, 2005 at 11:02 AM.
  12. #7
  13. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    If passive mode was enabled then the traceback should should show:

    File "C:\Python24\lib\ftplib.py", line 327, in ntransfercmd

    The active mode transfer is on line 334.
  14. #8
  15. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    I'm not so sure if you're sending the file correctly. But I could be wrong. Here's how I send my files, and it works just fine.
    Code:
    ftp.storbinary('STOR '+filename, open(filename))
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    Originally Posted by †Yegg†
    I'm not so sure if you're sending the file correctly. But I could be wrong. Here's how I send my files, and it works just fine.
    Code:
    ftp.storbinary('STOR '+filename, open(filename))
    Code:
    from ftplib import FTP
    ftp = FTP('www.someftp.org')
    ftp.set_pasv(True)
    ftp.login('Shiner_Man','password')
    '230 User Shiner_Man logged in.'
    filename = r'C:\test.txt'
    ftp.storbinary('STOR '+filename, open(filename))
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
      File "C:\Python24\lib\ftplib.py", line 415, in storbinary
        conn = self.transfercmd(cmd)
      File "C:\Python24\lib\ftplib.py", line 345, in transfercmd
        return self.ntransfercmd(cmd, rest)[0]
      File "C:\Python24\lib\ftplib.py", line 327, in ntransfercmd
        resp = self.sendcmd(cmd)
      File "C:\Python24\lib\ftplib.py", line 241, in sendcmd
        return self.getresp()
      File "C:\Python24\lib\ftplib.py", line 214, in getresp
        raise error_perm, resp
    error_perm: 550 Cannot STOR.
    Same thing. This must be on the server side.
    I'll learn this stuff someday.
  18. #10
  19. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Did you try your code with another ftp server?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    Originally Posted by †Yegg†
    Did you try your code with another ftp server?
    I don't have another ftp server that I have upload access to.
    I'll learn this stuff someday.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    Okay I figured out what the problem is(from http://support.bppresence.com/support/users/kb.php?id=10128):

    550 Cannot STOR. No permission. The problem here is with the path sent by the ftp client which is not relative to home directory, whereas server sent a relative path in PWD command. Solution is either to check if it is possible that the ftp client sends relative path or to disable "show path relative to home dir" option in user account (also in main server setup).
    Sure enough, this works:

    Code:
    from ftplib import FTP
    ftp = FTP('www.someftp.org')
    ftp.login('Shiner_Man','password')
    filename = 'test.txt'
    ftp.storbinary('STOR '+filename, open(filename))
    ftp.close()
    Now the problem is, I don't want the file to be in the same directory as the script. So now I have to do some messing around here.

    The issue is that the stor command can not have the full path so it should be:

    Code:
    ftp.storbinary('STOR test.txt', open(C:\test.txt))
    So I guess I have to figure out how to strip off the file name from the path.
    Last edited by Shiner_Man; June 27th, 2005 at 07:47 PM.
    I'll learn this stuff someday.
  24. #13
  25. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14

    It's funny how these things are obvious with 20-20 hindsight!

    grim
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    Originally Posted by Grim Archon

    It's funny how these things are obvious with 20-20 hindsight!

    grim
    Ain't that the truth.

    If I wanted to make this a function where you pass in the file to upload like 'C:\test.txt' what would be the easiest way to get the filename? Like test.txt rather then the full path. I guess I would have to search the string backwards and take everything from last backslash forward.

    This works:

    Code:
    import os
    file = r'C:\something\directory\anotherone\test.txt'
    os.path.split(file)[1]
    I'm not sure if thats the preferred way to do it but I guess I'll use it for now.
    Last edited by Shiner_Man; June 28th, 2005 at 07:01 AM.
    I'll learn this stuff someday.
  28. #15
  29. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Ooops missed the question earlier

    Your best solution would be to use os.path.basename:
    Code:
    >>> os.path.basename(r"c:\fred\test.txt")
    'test.txt'
    grim
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo