连接 RPC 服务器的安全方法

发布于 2024-10-12 18:09:44 字数 1800 浏览 2 评论 0原文

这个问题与 我们如何处理 Python xmlrpclib 连接被拒绝?

当我尝试使用以下代码时,在 RPC 服务器关闭的情况下,_get_rpc() 返回 False,我可以开始了。但是,如果服务器正在运行,则会以未知的方式失败。它是否尝试在远程服务器上执行 .connect() ?当我需要使用 .connect() 来检测返回的代理是否有效时,如何解决这个问题(请参阅相关问题)?

import xmlrpclib
import socket

def _get_rpc():
    try:
        a = xmlrpclib.ServerProxy('http://dd:[email protected]: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"

问题是这样的:

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'>

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 down, _get_rpc() returns False and I'm good to go. However if the server is running, it fails with unknown method. Is it trying to execute .connect() on the remote server? How can I get around this, when I needed to use .connect() to detect if the returned proxy worked (see related question)?

import xmlrpclib
import socket

def _get_rpc():
    try:
        a = xmlrpclib.ServerProxy('http://dd:[email protected]: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 issue:

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'>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚风撩人 2024-10-19 18:09:44

好吧,我只是在研究它;我的旧方法很糟糕,因为 xmlrpclib.ServerProxy 在调用方法时尝试连接到 XmlRPC 服务器,而不是之前!

试试这个:

import xmlrpclib
import socket

def _get_rpc():
    a = xmlrpclib.ServerProxy('http://dd:[email protected]: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)

总结一下,我们有 3 种情况:

  1. XmlRPC 服务器已启动,我们在其中定义了一个名为 _() 的方法

    编辑:我确实选择了名称_,因为它不太可能有使用此名称的方法,但这种情况仍然可能发生)
    在这种情况下,不会捕获异常并且代码将执行
    返回 True

  2. XmlRPC 服务器已启动,并且其中我们没有任何方法调用
    _()

    这次 xmlrpclib.Fault 将被引发,我们还将传递给 return True

  3. XmlRPC 服务器已关闭
    现在将引发 socket.error 异常,并且
    当我们调用 a._() 时,我们应该返回 False

我不知道是否有一种简单的方法可以做到这一点,在那之前我很乐意看到它,希望这次能解决这个问题:)

注意:当你执行 if a: 时,Python 将再次搜索方法 __nonzero__() 来测试 的布尔值>a 这将失败。

注意2:某些xmlrpc服务提供专门用于身份验证的rpc路径,在该路径中服务提供诸如login()之类的方法...,这种方法可以替换我们案例中的_()方法,因此只需调用login () 就足以知道服务是启动还是关闭 (socket.error),同时此 login() 方法会在服务启动时对用户进行身份验证。

Well i was just looking in to it ; my old method suck because xmlrpclib.ServerProxy try to connect to the XmlRPC server when you call a method, not before !!!

Try this instead :

import xmlrpclib
import socket

def _get_rpc():
    a = xmlrpclib.ServerProxy('http://dd:[email protected]: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 :

  1. XmlRPC server is up and in it we defined a method called _():

    (EDIT : i did choose the name _ because it unlikely to have a method with this name, but this case still can happen)
    In this case no exception will be catch and the code will execute
    the return True

  2. XmlRPC server is up and in it we don't have any method methoded call
    _()
    :
    This time xmlrpclib.Fault will be raised and we will also pass to the return True

  3. XmlRPC server is down:
    Now the socket.error exception will be raised and that
    when we call a._() so we should 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 thing this time :)

N.B: when you do if a: python will again search for a method __nonzero__() to test the boolean value of a and this will fail to.

N.B 2: Some xmlrpc service offer a rpc path specialized to do an authentication , in this path the service offer methods like login() ... , this kinds of method can replace the _() method in our case, so just calling login(), will be enough to know if the service is up or down (socket.error), and in the same time this login() method authenticate the user if the service is up .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文