How do we handle Python xmlrpclib Connection Refused?

I donโ€™t know what the hell I am doing wrong here, I wrote that the RPC client is trying to connect to a nonexistent server, and I am trying to handle the exception that is thrown, but it doesnโ€™t matter what I try, I cannot understand how I should Deal with it:

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" 

Since the server is down, I expect the output to be โ€œNo RPCโ€, but instead I get an exception:

 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 
+4
source share
1 answer

_get_rpc returns a reference to the unrelated method of the ServerProxy supervisor. An exception does not occur when calling _get_rpc, where you handle it; this happens when you try to evaluate this supervisor method (in "if not rpc"). Try the interactive prompt:

 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> 

Note how you see the exception when trying to evaluate the .supervisor method (ServerProxy (...). Supervisor, foo.supervisor or bar), but not only when you assign it elsewhere (bar = foo.supervisor).

+4
source

Source: https://habr.com/ru/post/1335624/


All Articles