我有一个 python 文件,其中包含我需要使用 jsonrpc 发布的函数。目前我可以将函数发布到所需的站点并在 python 中获得结果。但现在我想从 C# 运行 python 脚本,获取结果并对它们做一些事情。我在运行 python 脚本并将结果返回给 C# 时遇到问题
我不想下载 IronPython,因此不使用它的解决方案会很有帮助。
现在发生的事情是,当 Process.Start(start))
行被击中时,有一个 shell 会快速弹出然后消失。然后没有任何东西返回给读者。
Python 代码:
#!usr/bin/python
import sys
import json
import jsonrpclib
def dc_906(orderid, givexNum, amount):
jsonrpclib.config.use_jsonclass = True
server = jsonrpclib.Server('https://dev-dataconnect.com:50')
ping1 = server.dc_906('en', orderid, 'userid', 'password', num, amount)
print jsonrpclib.history.response #this could be a "return" instead of print, not sure.
if __name__ == "__main__":
function = sys.argv[1]
orderid = sys.argv[2]
num = sys.argv[3]
amount = sys.argv[4]
if function == 'dc_906':
dc_906(orderid, num, amount)
执行该过程的 C# 代码(来自:How do I run a Python script from C#?)
try
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"C:\Python27\python.exe"; //full path to python.exe
//start.FileName = @"C:\Windows\system32\cmd.exe";
//start.Arguments = string.Format("{0} {1} {2} {3}", @"C:\Users\J1035\Documents\Python27\GiveX_python\test.py", "123456789", "603628982592000186162", 20.00);
start.Arguments = string.Format("{0} {1}", @"C:\Users\J1035\Documents\Python27\GiveX_python\test.py", "123456789 603628982592000186162 20.00");
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using(Process process = Process.Start(start))
using (StreamReader reader = process.StandardOutput)
{
string foo = reader.ReadToEnd();
TxtResultOutput.Text += foo;
}
}
catch (Exception ex)
{
var foo = ex.Message;
}
在命令行上运行 python 脚本的结果:
最佳答案
您似乎忘记了参数行中的“dc_906”。没有它就不会调用您的函数。
关于c# - 执行python函数返回结果给c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22642975/