我不知道我在这里做错了什么,我写了一个RPC客户端试图连接到不存在的服务器,并且我试图处理引发的异常,但是无论我如何尝试我不知道该如何处理:
def _get_rpc():
try:
a = ServerProxy('http://dd:[email protected]:9001')
a = a.supervisor
return a
except:
return False
rpc = _get_rpc()
if not rpc:
print "No RPC"
由于没有服务器在运行,因此我希望输出为“No RPC”,但是却出现异常:
Traceback (most recent call last):
File "xmlrpctest.py", line 20, in <module>
if not rpc:
File "/usr/lib/python2.6/xmlrpclib.py", line 1199, in __call__
return self.__send(self.__name, args)
File "/usr/lib/python2.6/xmlrpclib.py", line 1489, in __request
verbose=self.__verbose
File "/usr/lib/python2.6/xmlrpclib.py", line 1235, in request
self.send_content(h, request_body)
File "/usr/lib/python2.6/xmlrpclib.py", line 1349, in send_content
connection.endheaders()
File "/usr/lib/python2.6/httplib.py", line 908, in endheaders
self._send_output()
File "/usr/lib/python2.6/httplib.py", line 780, in _send_output
self.send(msg)
File "/usr/lib/python2.6/httplib.py", line 739, in send
self.connect()
File "/usr/lib/python2.6/httplib.py", line 720, in connect
self.timeout)
File "/usr/lib/python2.6/socket.py", line 561, in create_connection
raise error, msg
socket.error: [Errno 111] Connection refused
最佳答案
_get_rpc返回对未连接的ServerProxy的 super 用户方法的引用。在处理_get_rpc的调用中不会发生该异常。当您尝试评估此 super 用户方法(在“如果不是rpc”中)时,就会发生这种情况。从交互式提示中尝试:
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import xmlrpclib
>>> xmlrpclib.ServerProxy("http://127.0.0.1");
<ServerProxy for 127.0.0.1/RPC2>
>>> xmlrpclib.ServerProxy("http://127.0.0.1").supervisor;
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/xmlrpclib.py", line 1199, in __call__
return self.__send(self.__name, args)
File "/usr/lib/python2.6/xmlrpclib.py", line 1489, in __request
verbose=self.__verbose
File "/usr/lib/python2.6/xmlrpclib.py", line 1243, in request
headers
xmlrpclib.ProtocolError: <ProtocolError for 127.0.0.1/RPC2: 404 Not Found>
>>> foo = xmlrpclib.ServerProxy("http://127.0.0.1");
>>> dir (foo)
['_ServerProxy__allow_none', '_ServerProxy__encoding', '_ServerProxy__handler', '_ServerProxy__host', '_ServerProxy__request', '_ServerProxy__transport', '_ServerProxy__verbose', '__doc__', '__getattr__', '__init__', '__module__', '__repr__', '__str__']
>>> foo.supervisor
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/xmlrpclib.py", line 1199, in __call__
return self.__send(self.__name, args)
File "/usr/lib/python2.6/xmlrpclib.py", line 1489, in __request
verbose=self.__verbose
File "/usr/lib/python2.6/xmlrpclib.py", line 1243, in request
headers
xmlrpclib.ProtocolError: <ProtocolError for 127.0.0.1/RPC2: 404 Not Found>
>>> bar = foo.supervisor
>>> bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/xmlrpclib.py", line 1199, in __call__
return self.__send(self.__name, args)
File "/usr/lib/python2.6/xmlrpclib.py", line 1489, in __request
verbose=self.__verbose
File "/usr/lib/python2.6/xmlrpclib.py", line 1243, in request
headers
xmlrpclib.ProtocolError: <ProtocolError for 127.0.0.1/RPC2: 404 Not Found>
请注意,在尝试评估.supervisor方法(ServerProxy(...)。supervisor,foo.supervisor或bar)时,如何看到异常,但在将其分配到其他位置(bar = foo.supervisor)时却没有看到。