1

I have python script that I can execute with no issues and get back my 200 code. I am having trouble validating that my C# code is sending the Id_Textbox.Text to the script, and getting the return code back to my label.

C#

string cmd = "C:/Python27/python.exe";
string args = String.Format(@"D:\Python Scripts\device#_put.py " + Id_TextBox.Text);

ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C:/Python27/python.exe";
start.Arguments = string.Format("{0} {1}", cmd, args);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
    using (StreamReader reader = process.StandardOutput)
    {
        string myString = reader.ReadToEnd();//Blank
        myString_Label.Text = myString;
    }

    process.WaitForExit();
}

And my tiny python script.

import httplib
Id = ''#want to pass in Id_TextBox.Text

headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 
'Authorization': 'apikey=#####'}
conn = httplib.HTTPSConnection('server1.net')
conn.request('POST', '/api/v1/device/' + Id, headers=headers)#Pass variable 
here.
resp = conn.getresponse()
print resp.status

Any help is appreciated!

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120

1 Answers1

0

You can use sys.argv :

import sys
#...
conn.request('POST', '/api/v1/device/' + sys.argv[1], headers=headers) #Pass variable

The script will receive it if you pass it as a first argument (python script.py ID), like you seem to be doing in your C# code.

Hassan
  • 609
  • 4
  • 9
  • Cool, I added that. Im still not getting anything back. – SenorInIssaquah Nov 30 '17 at 23:28
  • From what I can gather from the link that @MethodMan posted, you need to set the arguments like this : `start.Arguments = args;` instead of `start.Arguments = string.Format("{0} {1}", cmd, args);` – Hassan Nov 30 '17 at 23:38
  • Made that change and Im still getting nothing. :( – SenorInIssaquah Nov 30 '17 at 23:47
  • Does the Python script work if you test it by itself ? Try running it on the console to see if it outputs the correct result. You can also try running your C# code against a python script that only contains `print "Works"` to see if C# receives the "Works" string. Try replacing `"C:/Python27/python.exe"` with `@"C:\Python27\python.exe"`. I doubt it's what's causing this issue but it doesn't hurt to try. – Hassan Dec 01 '17 at 00:00