from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2','/')
# Create server
server = SimpleXMLRPCServer(("localhost", 8000),
requestHandler=RequestHandler)
server.register_introspection_functions()
# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(pow)
# Register a function under a different name
def adder_function(x,y):
return x + y
server.register_function(adder_function, 'add')
# Register an instance; all the methods of the instance are
# published as XML-RPC methods (in this case, just 'div').
class MyFuncs:
def div(self, x, y):
return x // y
server.register_instance(MyFuncs())
# Run the server's main loop
server.serve_forever()
var xmlrpc = require('xmlrpc')
var client = xmlrpc.createClient({ host: 'localhost', port: 8000, path: '/'})
// Sends a method call to the XML-RPC server
client.methodCall('pow', [2,2], function (error, value) {
// Results of the method response
console.log('Method response for \'anAction\': ' + value)
})
你要的是远程过程调用(RPCs)
您可以查看Python中的SimpleXMLRPCServer模块
服务器代码
Python客户端
^{pr2}$来源:https://docs.python.org/2/library/simplexmlrpcserver.html
编辑
XMLRPC是一个standard protocol,因此有它的实现 在大多数流行语言中。节点也有一个package。 你可以像这样用npm安装
npm install xmlrpc
您可以用它调用上面的python服务器
Javascript客户端
还有一个jQuery implementationxmlrpc。所以你可以从浏览器制作rpc。在
相关问题 更多 >
编程相关推荐