我正在编写一个脚本,其中脚本会读取很多部分[每个部分包含IP,用户名和密码]。我在这里面临例外问题。如果无法与任何服务器连接,我想打印正确的异常并转到下一部分。目前,如果网络无法访问,它就会停留在那里。有人可以帮助我在这里得到正确的例外吗?
代码:
def any_HE():
global config, logger
config = ConfigParser.RawConfigParser()
config.read('config.cfg')
for section in config.sections():
components = dict() #start with empty dictionary for each section
if not config.has_option(section, 'server.user_name'):
continue
env.user = config.get(section, 'server.user_name')
env.password = config.get(section, 'server.password')
host = config.get(section, 'server.ip')
try:
with settings(hide('warnings', 'running', 'stdout', 'stderr'),warn_only=True, host_string=host):
files = run('ls -ltr /opt/nds')
except Exception, e:
print e
配置文件是这样的:
[Logger]
#Possible values for logging are INFO, DEBUG and ERROR
log_level = DEBUG
[astro1a]
server.user_name = root
server.password = stains
server.ip = 172.19.40.21
[astro1b]
server.user_name = root
server.password = stains
server.ip = 172.19.41.22
在这里,我已经给出了IP测试的错误,输出如下。该计划永远不会退出。
输出:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/roradhak/Work/Scripts/ComponentListing/testLogging.py
Timed out trying to connect to 172.19.40.21 (tried 1 time)
No hosts found. Please specify (single) host string for connection: