skip Trackback error for ftp checking

M

moonhkt

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.
 
S

Steven D'Aprano

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.
 
M

moonhkt

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)
 
C

Chris Angelico

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top