【问题标题】:How to communicate between Python and C# using XML-RPC?如何使用 XML-RPC 在 Python 和 C# 之间进行通信?
【发布时间】:2010-11-08 14:44:07
【问题描述】:

假设我有一个用 Python 实现的简单 XML-RPC 服务:

from SimpleXMLRPCServer import SimpleXMLRPCServer  # Python 2

def getTest():
    return 'test message'

if __name__ == '__main__' :
    server = SimpleXMLRPCServer(('localhost', 8888))
    server.register_function(getTest)
    server.serve_forever()

谁能告诉我如何从 C# 调用 getTest() 函数?

【问题讨论】:

    标签: c# python xml-rpc


    【解决方案1】:

    感谢您的回答,我尝试了来自 darin 链接的 xml-rpc 库。我可以使用以下代码调用 getTest 函数

    using CookComputing.XmlRpc;
    ...
    
        namespace Hello
        {
            /* proxy interface */
            [XmlRpcUrl("http://localhost:8888")]
            public interface IStateName : IXmlRpcProxy
            {
                [XmlRpcMethod("getTest")]
                string getTest();
            }
    
            public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                }
                private void button1_Click(object sender, EventArgs e)
                {
                    /* implement section */
                    IStateName proxy = (IStateName)XmlRpcProxyGen.Create(typeof(IStateName));
                    string message = proxy.getTest();
                    MessageBox.Show(message);
                }
            }
        }
    

    【讨论】:

      【解决方案2】:

      不要自吹自擂,而是:http://liboxide.svn.sourceforge.net/viewvc/liboxide/trunk/Oxide.Net/Rpc/

      class XmlRpcTest : XmlRpcClient
      {
          private static Uri remoteHost = new Uri("http://localhost:8888/");
      
          [RpcCall]
          public string GetTest()
          {
              return (string)DoRequest(remoteHost, 
                  CreateRequest("getTest", null));
          }
      }
      
      static class Program
      {
          static void Main(string[] args)
          {
              XmlRpcTest test = new XmlRpcTest();
              Console.WriteLine(test.GetTest());
          }
      }
      

      这应该可以解决问题...注意,上面的库是 LGPL,它可能对你来说不够好,也可能不够好。

      【讨论】:

        【解决方案3】:

        为了从 c# 调用 getTest 方法,您需要一个 XML-RPC 客户端库。 XML-RPC 就是这样一个库的一个例子。

        【讨论】:

          猜你喜欢
          • 2011-12-03
          • 2012-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多