python xmlrpc对象是什么类型的?

时间:2013-01-12 14:39:58

标签: python types python-2.7 xml-rpc

我定义了一个python xmlrpc服务器如下(仅用于解释事物的近似示例):

from SimpleXMLRPCServer import SimpleXMLRPCServer
server = SimpleXMLRPCServer(('localhost', 8000))
server.register_function(foo, "serial.send")
server.serve_forever()

然后我可以使用xmlrpc客户端,如下所示

import xmlrpclib
device = xmlrpclib.ServerProxy("http://localhost:8000/RPC2")
device.serial.send(...)

在这里,我可以检查devicexmlrpclib.ServerProxy的实例

isinstance(device, xmlrpclib.ServerProxy)

但是device.serial是什么?我想检查device.serial是否属于xmlrpc,而不是serialsocket或其他内容。

以下是我想要检查的具体示例:

def foo(x):
   if isinstance(x, ...):
       print("xmlrpc access")
   else:
       print("direct access")

foo(device.serial) # expected output: 'xmlrpc access'
foo(serial.Serial(..)) # expected output: 'direct access'
foo(socket.Socket(...)) # expected outcome: 'direct access'

1 个答案:

答案 0 :(得分:0)

如果要求对Python对象进行任意检查,请使用'inspect' Python模块:

http://www.ibm.com/developerworks/library/l-pyint/index.html

还要检查

>> obj.__class__

>> obj.__class__.__bases__

允许您访问基类。