3

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

4
  • 1
    Try checking the ExitCode property to see if the script is completing successfully. Commented Mar 25, 2014 at 18:23
  • The Exit Code is 1. I was checking the comments from the article that I posted "okay, it works for me now. the problem is that you need to format the strings very carefully. any paths need "PATH" even if there are no spaces" not sure what this means. If anyone has an idea of what it that could help too. Commented Mar 25, 2014 at 18:26
  • Could you please try running the script with the arguments you are passing directly on the command line? Hopefully that will give us some clues as to what is wrong. Commented Mar 25, 2014 at 18:31
  • @Asad I added the results I get when I run the code on the command line. Commented Mar 25, 2014 at 18:37

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.