Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'subject:error': 0.03; 'see.': 0.07; 'exception,': 0.09; 'file)': 0.09; 'subject:messages': 0.09; 'try:': 0.09; 'subject:How': 0.10; 'displayed.': 0.16; 'file.close()': 0.16; 'filename,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ought': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'print': 0.22; 'error': 0.23; 'exists': 0.24; 'rid': 0.24; 'sort': 0.25; 'header :In-Reply-To:1': 0.27; 'point': 0.28; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'file': 0.32; 'running': 0.33; "i'd": 0.34; 'connection': 0.35; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'skip:f 40': 0.36; 'subject:?': 0.36; 'server': 0.38; 'nov': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'that,': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'removing': 0.60; 'first': 0.61; 'you.': 0.62; 'address': 0.63; 'show': 0.63; 'more': 0.64; 'different': 0.65; 'close': 0.67; 'invalid': 0.68; 'helping': 0.70; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=dcaYZb8yIRTMI3aOFmFDf4q/O7c6+opvowj1Hmd1VCk=; b=idJFlo/txKiXbhW1c8j3qUo+JJnzsThl8o+DNecJAwnTijq6lof8mYPEnMftrkx+SX XvYETUYnZx1hoCOU9M3TejxyLMieRjIgUmrzjyCFu+MLPJum8AhE/DiHMuFnkpsImbEo w7UHhSCcy6jbrDqUEDtJpYuUstaNlfCdpi6eYl8IGHnU8GbYfb6cC6MHzZUIkNxfkKnz dxmGYd07bTSZZK1kVpiSMSrLbIuIDWRrC00ZS1s8ZUh2ca+lQxRvP3XJQqZKFibmocZk qOlHTQh9brqiCc/aA/Om1dFQVxde4XUw4I2KLVhHlqKhZiZ+ohjGX6OzOyFA+Q85bOQP WPrg== MIME-Version: 1.0 X-Received: by 10.68.129.130 with SMTP id nw2mr18261002pbb.88.1384858481166; Tue, 19 Nov 2013 02:54:41 -0800 (PST) In-Reply-To: <6ad6e381-5f6b-434d-a82b-ff632358839f@googlegroups.com> References: <6ad6e381-5f6b-434d-a82b-ff632358839f@googlegroups.com> Date: Tue, 19 Nov 2013 21:54:41 +1100 Subject: Re: How to catch error messages in ftplib? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384858490 news.xs4all.nl 15962 [2001:888:2000:d::a6]:48168 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59979 On Tue, Nov 19, 2013 at 9:18 PM, 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? > 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