如何处理Python xmlrpclib连接被拒绝?
我不知道我到底哪里做错了。我写了一个RPC客户端,想要连接一个不存在的服务器,我在尝试处理抛出的异常,但无论我怎么尝试,都搞不清楚该怎么处理这个问题:
def _get_rpc():
try:
a = ServerProxy('http://dd:LNXFhcZnYshy5mKyOFfy@127.0.0.1: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
1 个回答
4
_get_rpc 返回的是一个未连接的 ServerProxy 的 supervisor 方法的引用。这个异常并不是在你处理 _get_rpc 调用的时候发生的,而是在你尝试评估这个 supervisor 方法的时候发生的(在 "if not 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),就不会出现异常。