XML-RPC C#与Python RPC服务器

5 投票
1 回答
3344 浏览
提问于 2025-04-15 15:08

在我的服务器上,我使用的是Python的标准示例(加了一个额外的Hello World方法),而在客户端我使用的是C#的XML-RPC.NET库。可是每次我运行客户端的时候,都会出现一个异常,提示找不到这个方法。有没有什么办法可以解决这个问题呢?

谢谢!

Python代码:

from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

# Create server
server = SimpleXMLRPCServer(("", 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')

def HelloWorld():
        return "Hello Henrik"

server.register_function(HelloWorld,'HelloWorld')

# 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()

C#代码:

namespace XMLRPC_Test
{
    [XmlRpcUrl("http://188.40.xxx.xxx:8000")]
    public interface HelloWorld : IXmlRpcProxy
    {
        [XmlRpcMethod]
        String HelloWorld();
    }
    [XmlRpcUrl("http://188.40.xxx.xxx:8000")]
    public interface add : IXmlRpcProxy
    {
        [XmlRpcMethod]
        int add(int x, int y);
    } 
    [XmlRpcUrl("http://188.40.xxx.xxx:8000")]
    public interface listMethods : IXmlRpcProxy
    {
        [XmlRpcMethod("system.listMethods")]  
        String listMethods();
    } 

    class Program
    {
        static void Main(string[] args)
        {
            listMethods proxy = XmlRpcProxyGen.Create<listMethods>();
            Console.WriteLine(proxy.listMethods());
            Console.ReadLine();
        }
    }
}

1 个回答

5

如果你把声明改成这样,能行吗?

[XmlRpcUrl("http://188.40.xxx.xxx:8000/RPC2")]

来自 Python 文档

SimpleXMLRPCRequestHandler.rpc_paths

这是一个属性值,必须是一个元组,里面列出可以接收 XML-RPC 请求的有效 URL 路径部分。如果请求发送到其他路径,就会出现 404 “没有这个页面”的错误。如果这个元组是空的,所有路径都会被认为是有效的。默认值是 ('/', '/RPC2')。

撰写回答