0

How can i send this list to list in Python script? This is so big for sending as arguments. Thank you.

 List<String> cefList= new List<String>();
               for(int i=0; i<1000; i++){
                cefList.Add("CEF:0|ArcSight|ArcSight|6.0.3.6664.0|agent:030|Agent [test] type [testalertng] started|Low| 
    eventId=1 mrt=1396328238973 categorySignificance=/Normal categoryBehavior=/Execute/Start 
    categoryDeviceGroup=/Application catdt=Security Mangement categoryOutcome=/Success 
    categoryObject=/Host/Application/Service art=1396328241038 cat=/Agent/Started 
    deviceSeverity=Warning rt=1396328238937 fileType=Agent 
    cs2=<Resource ID\="3DxKlG0UBABCAA0cXXAZIwA\=\="/> c6a4=fe80:0:0:0:495d:cc3c:db1a:de71 
    cs2Label=Configuration Resource c6a4Label=Agent 
    IPv6 Address ahost=SKEELES10 agt=888.99.100.1 agentZoneURI=/All Zones/ArcSight 
    System/Private Address Space 
    Zones/RFC1918: 888.99.0.0-888.200.255.255 av=6.0.3.6664.0 atz=Australia/Sydney 
    aid=3DxKlG0UBABCAA0cXXAZIwA\=\= at=testalertng dvchost=SKEELES10 dvc=888.99.100.1 
    deviceZoneURI=/All Zones/ArcSight System/Private Address Space Zones/RFC1918: 
    888.99.0.0-888.200.255.255 dtz=Australia/Sydney _cefVer=0.1");
                }
3
  • 1
    Use sockets to pass data between them Commented Aug 16, 2016 at 6:31
  • The simplest way is to print it to stdout in C# and use an anonymous pipe to read the data in python. But it does depend on the relationship between the two processes. Does the C# run the python, does python run the C#, or are they unrelated? Commented Aug 16, 2016 at 6:49
  • C# run the Python script. Commented Aug 16, 2016 at 7:07

2 Answers 2

3

Since your C# program runs the python script, I guess the easiest solution would be to redirect the standard input of the python process:

     Process pyProc = Process.Start(
        new ProcessStartInfo("python.exe", @"/path/to/the/script.py")
        {
           RedirectStandardInput = true,
           UseShellExecute = false
        }
     );
     for (int ii = 0; ii < 100; ++ii)
     {
        pyProc.StandardInput.WriteLine(string.Format("this is message # {0}", ii));
     }

At the python script side, you just need to use built-in function raw_input like below (please note the function has been renamed to raw_input in 3.x):

while True:
    data = raw_input()
    print(data)
Sign up to request clarification or add additional context in comments.

14 Comments

And how to get this in Python?
raw_input is renamed to input. But it doesn't work while True: data = input() with open("C:/Users/korisnik/Desktop/file.txt", "w") as text_file: text_file.write(data)
I don't have 3.x environment handy. But the code posted above did run in 2.7
By reading your code in the comment above, I guess the problem was not at function input, instead it was at the mode 'w' you used in opening file.txt. You may use mode 'w+' to avoid overwitting. A better way is to move the with scope out of while scope, in which you will not have to open the file repeatly
@user3661837, the problem in your code is the use of indentation. For having one piece of code in another's scope, you have to indenting it.
|
2

You need to serialize the data to a common format that is accessible from both C# and Python. For example - XML or JSON. I would recommend using JSON.

Then you have several options:

  • Use sockets to transfer the data.
  • Use http to transfer the data.
  • Write to a file from C# and read that file from Python

Sockets would probably be faster. Using http might be easier. With files, you will need to have some sort of scheduling or notification system to let your Python program know when you have written to the file.

4 Comments

Does anyone have a example? I am new with Python.
Do you know how to convert data to json from c#? In python, you can parse a json string using the json module. import json first and then data = json.loads("the json string")
[["first"],["second"]] , like this.
That doesn't look like valid JSON. This might help - masnun.com/2011/07/08/quick-json-parsing-with-c-sharp.html

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.