繁体   English   中英

在python xmlrpc代理上使用getattr

[英]using getattr on a python xmlrpc proxy

这个答案暗示我可以在xmlrpc服务器代理上使用getattr。 但是,就我而言,这似乎不起作用。 我是否缺少服务器定义步骤?

我的服务器代码是这样的:

import SimpleXMLRPCServer
class Listener(object):
    def ping(self):
        return "ping"
s = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost",8910), allow_none=True)
l = Listener()
s.register_instance(l)
s.register_introspection_functions()
s.serve_forever()

以交互方式使用它时,可以调用ping函数,但不能在代理上使用getattr:

$ python -i
ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:22:14) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import xmlrpclib
>>> proxy = xmlrpclib.ServerProxy("http://localhost:8910", allow_none=True)
>>> proxy.ping()
'ping'
>>> getattr(proxy, "ping")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python27\lib\xmlrpclib.py", line 1224, in __call__
    return self.__send(self.__name, args)
  File "c:\Python27\lib\xmlrpclib.py", line 1575, in __request
    verbose=self.__verbose
  File "c:\Python27\lib\xmlrpclib.py", line 1264, in request
    return self.single_request(host, handler, request_body, verbose)
  File "c:\Python27\lib\xmlrpclib.py", line 1297, in single_request
    return self.parse_response(response)
  File "c:\Python27\lib\xmlrpclib.py", line 1473, in parse_response
    return u.close()
  File "c:\Python27\lib\xmlrpclib.py", line 793, in close
    raise Fault(**self._stack[0])
xmlrpclib.Fault: <Fault 1: '<type \'exceptions.Exception\'>:method "ping.__repr__" is not supported'>
>>> 

在交互式控制台中,这种方式不起作用。 控制台希望显示该函数的repr ,但尝试调用ping.__repr__失败。

分配给变量的工作原理是:

>>> f = getattr(proxy, 'ping')
>>> f()
'ping'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM