XML RPC - Call python functions from C#

限于喜欢 提交于 2019-12-06 05:36:58

问题


I am using lib xml-rpc.net.2.5.0 for creating a XML RPC client in C# which invokes some python methods. The client is located in a Windows7 machine and the server is located in VMWare running red hat.

Client code which invokes python function (code inside main method):

IRemoteKillTest iRemoteKillTest = XmlRpcProxyGen.Create<IRemoteKillTest>();

int result = iRemoteKillTest.kill();
Console.WriteLine("Result = " + result);
Console.ReadLine();

Client Interface:

[XmlRpcUrl("http://192.ZZZ.YYY.XXX:8000/RPC2")]
public interface IRemoteKillTest : IXmlRpcProxy
{
    [XmlRpcMethod]
    int kill();
}

[XmlRpcUrl("http://192.ZZZ.YYY.XXX:8000/RPC2")]
public interface IRemoteMsgTest : IXmlRpcProxy
{
    [XmlRpcMethod]
    string msg();
}

Server code:

from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
import subprocess

class RequestHandler(SimpleXMLRPCRequestHandler):
       rpc_paths = ('/RPC2',)

server = SimpleXMLRPCServer(("192.ZZZ.YYY.XXX", 8000), requestHandler=RequestHandler)
server.register_introspection_functions()

class MyFuncs:
    def msg(self):
        return "It Works!"

    def kill(self):
        status = subprocess.call("ps -ef | grep <my_grep_info>  | grep -v grep | awk  '{ print $2 }' | xargs kill", shell=True)
        return status

 server.register_instance(MyFuncs())
 server.serve_forever()

If the client calls function msg from python it is executed successfully and the string is returned to c# but if it calls function kill i got the following exception:

XmlRpcFaultException:
Server returned a fault exception: [1] exceptions.Exception:method "kill" is not supported

It happens for any other python method like os.listdir, subprocess.Popen and so on. I have made a test calling kill method from a python client running inside VMWare red hat and it works.

I need a help as soon as possible, so pls if anyone has already faced it and have a explanation for that it will be very usefull. Thanks in advance and regards,

来源:https://stackoverflow.com/questions/22674769/xml-rpc-call-python-functions-from-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!