skip Trackback error for ftp checking

Discussion in 'Python' started by moonhkt, Nov 10, 2012.

  1. moonhkt

    moonhkt Guest

    HI All

    How to skip Trackback warning/error when input ftp address is not
    correct or reject ?

    AIX 5.3

    from ftplib import FTP
    import ftplib
    import sys
    from optparse import OptionParser

    parser = OptionParser()

    parser.add_option("-a","--remote_host_address",
    dest="remote_host_address",
    help="REMOTE FTP HOST.",metavar="REMOTE FTP HOST")

    parser.add_option("-u","--username", dest="username",
    help="USERNAME for ftp sever.",metavar="USERNAME")

    parser.add_option("-p","--password", dest="password",
    help="PASSWORD for ftp server.",metavar="PASSWORD")

    (options, args ) = parser.parse_args ()

    if not (options.remote_host_address):
    parser.error("REMOTE HOST are mandatory")

    if options.username and not options.password:
    parser.error("PASSWORD is mandatory if USERNAME is present")

    try:
    ftp = FTP(options.remote_host_address)
    except ftplib.error_perm,e:
    sys.exit(2)

    if options.username:
    try:
    ftp.login(options.username,options.password)
    except ftplib.error_perm,e:
    print "Login failed: %s" % e
    sys.exit(1)
    else:
    try:
    ftp.login()
    except ftplib.error_perm,e:
    print "Anonymous login failed: %s" % e
    sys.exit(1)
    try:
    print "LOGIN OK"
    finally:
    ftp.close()

    Command line
    -------------------------------
    chkftp.py -a teseting

    Output as below
    ----------------------------
    Traceback (most recent call last):
    File "...chkftp.py", line 33, in <module>
    ftp = FTP(options.remote_host_address)
    File "/opt/freeware/lib/python2.6/ftplib.py", line 116, in __init__
    self.connect(host)
    File "/opt/freeware/lib/python2.6/ftplib.py", line 131, in connect
    self.sock = socket.create_connection((self.host, self.port),
    self.timeout)
    File "/opt/freeware/lib/python2.6/socket.py", line 498, in
    create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
    socket.gaierror: [Errno 2] temporary failure in name resolution.
    moonhkt, Nov 10, 2012
    #1
    1. Advertising

  2. On Fri, 09 Nov 2012 20:51:47 -0800, moonhkt wrote:

    > HI All
    >
    > How to skip Trackback warning/error when input ftp address is not
    > correct or reject ?


    The same way you would skip any other error when you do something wrong:
    catch the exception.


    --
    Steven
    Steven D'Aprano, Nov 10, 2012
    #2
    1. Advertising

  3. moonhkt

    moonhkt Guest

    On Nov 10, 2:50 pm, Steven D'Aprano <steve
    > wrote:
    > On Fri, 09 Nov 2012 20:51:47 -0800, moonhkt wrote:
    > > HI All

    >
    > > How to skip Trackback warning/error when input ftp address is not
    > > correct or reject ?

    >
    > The same way you would skip any other error when you do something wrong:
    > catch the exception.
    >
    > --
    > Steven


    Thank. Added below.
    try:
    ftp = FTP(options.remote_host_address)
    except :
    print "Host address not found."
    sys.exit(1)
    moonhkt, Nov 12, 2012
    #3
  4. On Mon, Nov 12, 2012 at 8:25 PM, moonhkt <> wrote:
    > On Nov 10, 2:50 pm, Steven D'Aprano <steve
    > > wrote:
    >>
    >> The same way you would skip any other error when you do something wrong:
    >> catch the exception.

    >
    > Thank. Added below.
    > try:
    > ftp = FTP(options.remote_host_address)
    > except :
    > print "Host address not found."
    > sys.exit(1)


    Heh, I'm afraid that's not quite what Steven meant; what you're doing
    there is literally skipping _any other error_. A bare except is
    usually not a good thing. Catch the specific exception you want to
    catch - that way, if you typo a name or something, you don't get an
    obscure message about the host address when it's really a coding bug.

    ChrisA
    Chris Angelico, Nov 12, 2012
    #4
    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. Mark Olbert
    Replies:
    2
    Views:
    1,941
    Mark Olbert
    Dec 5, 2003
  2. Replies:
    3
    Views:
    5,601
  3. Jeff Miller
    Replies:
    0
    Views:
    471
    Jeff Miller
    Mar 26, 2009
  4. Patrick Lajeunesse
    Replies:
    0
    Views:
    721
    Patrick Lajeunesse
    Jul 10, 2009
  5. D. Buck
    Replies:
    2
    Views:
    465
    D. Buck
    Jun 29, 2004
Loading...

Share This Page