执行python函数将结果返回给c#
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了执行python函数将结果返回给c#,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1800字,纯文字阅读大概需要3分钟。
内容图文
![执行python函数将结果返回给c#](/upload/InfoBanner/zyjiaocheng/660/b2c95a18583f40c19096850386a49a81.jpg)
我有一个python文件,其中包含需要使用jsonrpc发布的函数.目前,我可以将函数发布到所需的站点,并在python中获得结果.但是现在我想从C#运行python脚本,获取结果并对其进行处理.我在运行python脚本并将结果返回到C#时遇到麻烦
我不想下载IronPython,因此不使用它的解决方案将有所帮助.
现在发生的事情是,有一个外壳会快速弹出,然后在按下Process.Start(start))行时消失.然后什么也没有返回给阅读器.
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”.没有它,就不会调用您的函数.
内容总结
以上是互联网集市为您收集整理的执行python函数将结果返回给c#全部内容,希望文章能够帮你解决执行python函数将结果返回给c#所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。