在Python中处理从其他库导入的库引发异常的适当方法是什么?
例如,我有一个名为“pycontrol”的库,它被导入到我的主程序中。 “pycontrol”导入了“suds”库。而“suds”库则导入了“urllib2”库。我注意到当“suds”库在访问通过“urllib2”访问的远程资源时遇到问题时,这些异常会向上传递到我的主程序中。
我目前最好的猜测是将urllib2和suds导入到全局命名空间中,并捕获它们抛出的典型异常,以及在“pycontrol”中未处理的异常。
还有其他最佳实践吗,可以用来解决这个问题?
以下是代码片段的基本想法(不导入suds或urllib2到全局命名空间):
以下是一个示例回溯:
例如,我有一个名为“pycontrol”的库,它被导入到我的主程序中。 “pycontrol”导入了“suds”库。而“suds”库则导入了“urllib2”库。我注意到当“suds”库在访问通过“urllib2”访问的远程资源时遇到问题时,这些异常会向上传递到我的主程序中。
我目前最好的猜测是将urllib2和suds导入到全局命名空间中,并捕获它们抛出的典型异常,以及在“pycontrol”中未处理的异常。
还有其他最佳实践吗,可以用来解决这个问题?
以下是代码片段的基本想法(不导入suds或urllib2到全局命名空间):
import pycontrol.pycontrol as pc
print "Connecting to iControl API on LTM %s..." % ltm
try:
b = pc.BIGIP(hostname=ltm, username=user, password=pw,
wsdls=wsdl_list, fromurl=True,
debug=soap_debug)
except (<whattocatch>), detail:
print "Error: could not connect to iControl API on LTM %s... aborting!" % ltm
print "Details: %s" % detail
exitcode = 1
else:
print "Connection successfully established."
以下是一个示例回溯:
Connecting to iControl API on LTM s0-bigip1-lb2.lab.zynga.com...
Traceback (most recent call last):
File "./register.py", line 507, in <module>
main()
File "./register.py", line 415, in main
b = build_bigip_object(ltm, user, pw, WSDLS, soap_debug = False)
File "./register.py", line 85, in build_bigip_object
debug=soap_debug)
File "build/bdist.macosx-10.6-universal/egg/pycontrol/pycontrol.py", line 81, in __init__
File "build/bdist.macosx-10.6-universal/egg/pycontrol/pycontrol.py", line 103, in _get_clients
File "build/bdist.macosx-10.6-universal/egg/pycontrol/pycontrol.py", line 149, in _get_suds_client
File "/Library/Python/2.6/site-packages/suds/client.py", line 111, in __init__
self.wsdl = reader.open(url)
File "/Library/Python/2.6/site-packages/suds/reader.py", line 136, in open
d = self.fn(url, self.options)
File "/Library/Python/2.6/site-packages/suds/wsdl.py", line 136, in __init__
d = reader.open(url)
File "/Library/Python/2.6/site-packages/suds/reader.py", line 73, in open
d = self.download(url)
File "/Library/Python/2.6/site-packages/suds/reader.py", line 88, in download
fp = self.options.transport.open(Request(url))
File "/Library/Python/2.6/site-packages/suds/transport/https.py", line 60, in open
return HttpTransport.open(self, request)
File "/Library/Python/2.6/site-packages/suds/transport/http.py", line 62, in open
return self.u2open(u2request)
File "/Library/Python/2.6/site-packages/suds/transport/http.py", line 118, in u2open
return url.open(u2request, timeout=tm)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 383, in open
response = self._open(req, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 401, in _open
'_open', req)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 361, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 1138, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 1105, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 8] nodename nor servname provided, or not known>