我有一個python文件填充了我需要使用jsonrpc發佈的函數。目前我可以發佈功能到所需的網站,並獲得python結果。但是現在我想從C#運行Python腳本,獲取結果並對它們做些什麼。我有麻煩讓python腳本運行並將結果返回給C#執行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腳本:
嘗試檢查'ExitCode'屬性以查看腳本是否成功完成。 –
退出代碼是1.我正在檢查文章中的評論,我發佈了「好的,現在對我有用,問題是你需要非常仔細地格式化字符串,任何路徑都需要」PATH「,即使有沒有空間「不確定這是什麼意思。如果有人知道它可以提供什麼幫助。 –
您可以嘗試使用直接在命令行上傳遞的參數來運行腳本嗎?希望這會給我們一些線索,告訴我們什麼是錯的。 –