Thread: ftp

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    346
    Rep Power
    0

    ftp


    how can i upload an image file to my server using ftp?

    thanks.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    17
    Rep Power
    0
    I'm not going to say rtfm http://www.python.org/doc/2.3.4/lib/module-ftplib.html
    Since it's an image file you'll want to transfer in binary mode.

    cheers
    ktpr
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    346
    Rep Power
    0
    i did rtfm but it is ****e. there is nothing about loogin into ftp servers with user name and password, nothing about putting a file on the server (well there mite be) and nothing about image files. it sucks ***.
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    I've no idea what you were reading
    class FTP( [host[, user[, passwd[, acct]]]])

    Return a new instance of the FTP class. When host is given, the method call connect(host) is made. When user is given, additionally the method call login(user, passwd, acct) is made (where passwd and acct default to the empty string when not given).
    And on the next page

    storbinary( command, file[, blocksize])

    Store a file in binary transfer mode. command should be an appropriate "STOR" command: "STOR filename". file is an open file object which is read until EOF using its read() method in blocks of size blocksize to provide the data to be stored. The blocksize argument defaults to 8192. Changed in version 2.1: default for blocksize added.

    quit( )

    Send a "QUIT" command to the server and close the connection. This is the ``polite'' way to close a connection, but it may raise an exception of the server reponds with an error to the "QUIT" command. This implies a call to the close() method which renders the FTP instance useless for subsequent calls (see below).
    And here is a simple sample:
    Code:
    from ftplib import FTP
    
    myftp = FTP("host url", "username", "password")
    myftp.cwd("path_where_you_write_file") #if needed
    
    myfile = open("myfilename",'rb') #binary  :D
    myftp.storbinary("STOR myfilename",myfile)
    myfile.close()
    
    myftp.quit()
    That's all there is to it (without exception handling which you should consider in the design)

    As a general solution, the areas likely to fail are:
    FTP session cannot be created - bad parameters - no link etc
    cwd fail - path doesnt exist, permission failure
    storbinary fails - permission failure
    file doenst exist, is corrupt, no read permission.

    and general IP problems with bad links.

    Bottom line - design the code to handle failure


    grimey
    Last edited by Grim Archon; December 14th, 2004 at 08:35 AM. Reason: oops - binary mode for reading file

IMN logo majestic logo threadwatch logo seochat tools logo