我们如何处理Python xmlrpclib连接被拒绝?

时间:2011-01-14 22:24:52

标签: python exception-handling xml-rpc 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 个答案:

答案 0 :(得分: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)。