user3661837
user3661837

Reputation: 95

How to send data from C# to Python

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");
                }

Upvotes: 0

Views: 8226

Answers (2)

masnun
masnun

Reputation: 11906

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.

Upvotes: 2

Lei Shi
Lei Shi

Reputation: 767

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)

Upvotes: 3

Related Questions