B-M
B-M

Reputation: 1288

execute python functions return results to c#

I have a python file filled with functions that I need to post using jsonrpc. Currently I can post the functions to the desired site and get results in python. But now I want to run the the python script from C#, get the results and do something with them. I am having troubles getting the python script to run and return the results to C#

I prefer to not download IronPython, so a solution that doesn't use it would be helpful.

What happens now is it there is a shell that pop ups quick then disappears when the Process.Start(start)) line is hit. Then nothing is returned to the reader.

Python Code:

#!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# code to execute the process (gotten from: 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;
}

Results from running the python script on the command line:

enter image description here

Upvotes: 3

Views: 5417

Answers (1)

Kenny Anderson
Kenny Anderson

Reputation: 91

It looks like you're forgetting the "dc_906" in your arguments line. Your function isn't being called without it.

Upvotes: 4

Related Questions