Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59975 > unrolled thread

How to catch error messages in ftplib?

Started byJL <lightaiyee@gmail.com>
First post2013-11-19 02:18 -0800
Last post2013-11-19 21:54 +1100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  How to catch error messages in ftplib? JL <lightaiyee@gmail.com> - 2013-11-19 02:18 -0800
    Re: How to catch error messages in ftplib? JL <lightaiyee@gmail.com> - 2013-11-19 02:20 -0800
    Re: How to catch error messages in ftplib? Chris Angelico <rosuav@gmail.com> - 2013-11-19 21:54 +1100

#59975 — How to catch error messages in ftplib?

FromJL <lightaiyee@gmail.com>
Date2013-11-19 02:18 -0800
SubjectHow to catch error messages in ftplib?
Message-ID<6ad6e381-5f6b-434d-a82b-ff632358839f@googlegroups.com>
I have the following code;

    try:
        session = FTP(ftp_server_ip,ftp_user,ftp_password)
        file = open(filename,'rb') # file to send    
        session.storbinary('STOR ' +  filename, file) # send the file
    except Exception, errObj:
        print Exception
        print errObj
    file.close() # close file and FTP
    session.quit()

I deliberately placed an invalid ip address for the ftp_server_ip to see whether error messages can be caught. However, no exception was thrown. Can someone more experienced point to me what did I do wrong?

Thank you.

[toc] | [next] | [standalone]


#59976

FromJL <lightaiyee@gmail.com>
Date2013-11-19 02:20 -0800
Message-ID<dca243d2-a3dd-49bf-afcc-70335dd8a022@googlegroups.com>
In reply to#59975
I repost the original code segment to make it more complete;

    from ftplib import FTP
    try:
        session = FTP(ftp_server_ip,ftp_user,ftp_password)
        file = open(filename,'rb') # file to send    
        session.storbinary('STOR ' +  filename, file) # send the file
    except Exception, errObj:
        print Exception
        print errObj
    file.close() # close file and FTP
    session.quit()

On Tuesday, November 19, 2013 6:18:07 PM UTC+8, JL wrote:
> I have the following code;
> 
> 
> 
>     try:
> 
>         session = FTP(ftp_server_ip,ftp_user,ftp_password)
> 
>         file = open(filename,'rb') # file to send    
> 
>         session.storbinary('STOR ' +  filename, file) # send the file
> 
>     except Exception, errObj:
> 
>         print Exception
> 
>         print errObj
> 
>     file.close() # close file and FTP
> 
>     session.quit()
> 
> 
> 
> I deliberately placed an invalid ip address for the ftp_server_ip to see whether error messages can be caught. However, no exception was thrown. Can someone more experienced point to me what did I do wrong?
> 
> 
> 
> Thank you.

[toc] | [prev] | [next] | [standalone]


#59979

FromChris Angelico <rosuav@gmail.com>
Date2013-11-19 21:54 +1100
Message-ID<mailman.2905.1384858490.18130.python-list@python.org>
In reply to#59975
On Tue, Nov 19, 2013 at 9:18 PM, JL <lightaiyee@gmail.com> wrote:
> I have the following code;
>
>     try:
>         session = FTP(ftp_server_ip,ftp_user,ftp_password)
>         file = open(filename,'rb') # file to send
>         session.storbinary('STOR ' +  filename, file) # send the file
>     except Exception, errObj:
>         print Exception
>         print errObj
>     file.close() # close file and FTP
>     session.quit()
>
> I deliberately placed an invalid ip address for the ftp_server_ip to see whether error messages can be caught. However, no exception was thrown. Can someone more experienced point to me what did I do wrong?
>

My first suggestion would be to get rid of the try/except block - it's
not really helping you. Just let the exception be displayed. When I
try that, I get a variety of different errors, depending on what sort
of "invalid IP address" was used - if it's malformed
("192.168.1.2.3"), I get a DNS failure, if it's a computer that
doesn't exist but ought to be on my LAN ("192.168.0.2"), I get a
timeout, and if it's one that exists but doesn't have an FTP server
running ("192.168.0.3"), I get a connection refusal. Exactly what I'd
expect to see. Removing the try/except will show what's happening.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web