Best way to trap errors in ftplib?

Discussion in 'Python' started by Peter A. Schott, Jan 11, 2005.

  1. Using ftplib.FTP object for a project we have here to upload/download files. I
    know that I can wrap everything in try/except blocks, but I'm having trouble
    getting the exact error messages out of the Exceptions.

    I'd like to either send an e-mail or log the failure to a database. It would
    also be nice to see what the message is and perhaps add some additional logic
    for delay/retry or something similar.

    Has anyone done any of the above? Willing to share some samples?

    Here's a little of my code - maybe you can see what I'm doing/not doing.

    def myuploadbinaryfunction(args here):
    objFTP = ftplib.FTP(HostName, UserName, Password)
    objFTP.cwd(RemotePath)

    try:
    objFTP.storbinary("STOR " + RemoteFile, file(os.path.join(LocalPath,
    LocalFile), "rb"))
    SQLLogging.LogFTP(LocalFile, Company, 1)
    except Exception, errObj:
    SQLLogging.LogFTP(LocalFile, Company, 0)
    reportError("Failed to upload " + LocalFile, str(errObj))
    print Exception
    print errObj



    In the above, reportError will send an e-mail to alert. I realize that the
    errObj in this case may not have the ability to convert to string, but am not
    sure how to get the output otherwise. Not sure how to handle a retry of a
    failure, either.

    Thank you for your help!

    -Peter Schott
     
    Peter A. Schott, Jan 11, 2005
    #1
    1. Advertising

  2. Peter A.Schott wrote:

    >Using ftplib.FTP object for a project we have here to upload/download files. I
    >know that I can wrap everything in try/except blocks, but I'm having trouble
    >getting the exact error messages out of the Exceptions.
    >
    >

    Consider using the traceback a la:

    try:
    [... whatever ...]
    except:
    import sys, traceback
    t, v, tb = sys.exc_info()
    # or use StringIO to "print" the traceback to and then log *that*
    traceback.print_tb(tb)

    // m
     
    Mark McEahern, Jan 12, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sean Nolan

    Trap "connection pool" errors

    Sean Nolan, Jul 11, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    396
    David Browne
    Jul 11, 2003
  2. Andrew Banks

    Trap all errors

    Andrew Banks, Apr 22, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    425
    Rick Spiewak
    Apr 23, 2004
  3. Rolf Kemper

    Trap run time errors in XSLT

    Rolf Kemper, Oct 20, 2004, in forum: XML
    Replies:
    0
    Views:
    483
    Rolf Kemper
    Oct 20, 2004
  4. SalP
    Replies:
    3
    Views:
    404
    bruce barker \(sqlwork.com\)
    May 17, 2006
  5. Bakes

    Odd/Weird errors with FTPLib

    Bakes, Sep 13, 2009, in forum: Python
    Replies:
    6
    Views:
    399
    Bakes
    Sep 15, 2009
Loading...

Share This Page