Secure way to connect to an RPC server

This question is related to How do we handle Python xmlrpclib Connection Refused?

When I try to use the following code with my RPC server, _get_rpc () returns False, and I'm good to go. However, if the server is running, it is not working with an unknown method. Is it trying to execute .connect () on a remote server? How can I get around this when I needed to use .connect () to determine if the returned proxy worked (see the related question)?

import xmlrpclib import socket def _get_rpc(): try: a = xmlrpclib.ServerProxy('http://dd: LNXFhcZnYshy5mKyOFfy@127.0.0.1 :9001') a.connect() # Try to connect to the server return a.supervisor except socket.error: return False if not _get_rpc(): print "Failed to connect" 

Here is the problem:

 ahiscox@lenovo :~/code/dd$ python xmlrpctest2.py Failed to connect ahiscox@lenovo :~/code/dd$ supervisord -c ~/.supervisor # start up RPC server ahiscox@lenovo :~/code/dd$ python xmlrpctest2.py Traceback (most recent call last): File "xmlrpctest2.py", line 13, in <module> if not _get_rpc(): File "xmlrpctest2.py", line 7, in _get_rpc a.connect() # Try to connect to the server 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 1253, in request return self._parse_response(h.getfile(), sock) File "/usr/lib/python2.6/xmlrpclib.py", line 1392, in _parse_response return u.close() File "/usr/lib/python2.6/xmlrpclib.py", line 838, in close raise Fault(**self._stack[0]) xmlrpclib.Fault: <Fault 1: 'UNKNOWN_METHOD'> 
+4
source share
1 answer

Well, I just looked at him; my old method is suck because xmlrpclib.ServerProxy try to connect to the XmlRPC server when you call the method, not before !!!

Try this instead:

 import xmlrpclib import socket def _get_rpc(): a = xmlrpclib.ServerProxy('http://dd: LNXFhcZnYshy5mKyOFfy@127.0.0.1 :9001') try: a._() # Call a fictive method. except xmlrpclib.Fault: # connected to the server and the method doesn't exist which is expected. pass except socket.error: # Not connected ; socket error mean that the service is unreachable. return False, None # Just in case the method is registered in the XmlRPC server return True, a connected, server_proxy = _get_rpc(): if not connected print "Failed to connect" import sys sys.exit(1) 

To summarize this, we have 3 cases:

  • An XmlRPC server is inserted, and in it we have defined a method called _ () :
    ( EDIT : I chose the name _ because it is unlikely to have a method with that name, but this case can still happen)
    In this case, the exception will not catch, and the code will be executed return True

  • The XmlRPC server is started, and in it we do not have a method controlled by the _ () method:
    This time xmlrpclib.Fault will be raised and we will also move on to return True

  • XmlRPC server does not work :
    Now the socket.error exception will be raised and that when we call a._() , so we must return False

I don’t know if there is an easy way to do this, and I will love to see it until then, hope this can fix it this time :)

NB: when you do if a: python will again look for the __nonzero__() method to check the boolean value of a , and that will fail.

NB 2: Some xmlrpc services offer an authentication-specific rpc path, along with service delivery methods such as login () ..., this method can replace the _ () method in our case, so just call login () will be enough to find out whether the service (socket.error) is up or down, and at the same time, this login () method authenticates the user if the service is completed.

+4
source

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


All Articles