Question

I would like to pass binary information between Python and C#. I would assume that you can open a standard in/out channel and read and write to that like a file, but there are a lot of moving parts, and I don't know C# too well. I want to do this sort of thing, but without writing a file.

# python code
with open(DATA_PIPE_FILE_PATH, 'wb') as fid:
    fid.write(blob)
subprocess.Popen(C_SHARP_EXECUTABLE_FILE_PATH)
with open(DATA_PIPE_FILE_PATH, 'rb') as fid:
    'Do stuff with the data'

// C# code
static int Main(string[] args){
    byte[] binaryData = File.ReadAllBytes(DataPipeFilePath);
    byte[] outputData;
    // Create outputData
    File.WriteAllBytes(outputData)

I've tried several different ways of using standard in/out, but I've had no luck matching them up, like I said, there are a lot of moving parts. I've tried things like

p = subprocess.Popen(C_SHARP_EXECUTABLE_FILE_PATH, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write(blob)
p.stdin.close()

or

p = subprocess.Popen(C_SHARP_EXECUTABLE_FILE_PATH, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = p.communicate(blob)

on the python side along with

TextReader tIn = Console.In;
TextWriter tOut = Console.Out;
String str = tIn.ReadToEnd();
//etc...

as well as a couple of other things that didn't work on the C# side. I've had mild success with some things, but I've changed it around so much that I don't remember what has worked for what. Could somebody give me a hint as to which pieces would work the best, or if this is even possible?

The data I want to pass has null and other non-printable characters.

Was it helpful?

Solution

This python code was correct

p = Popen(C_SHARP_EXECUTABLE_FILE_PATH, stdout=PIPE, stdin=PIPE, stderr=PIPE)
out, err = p.communicate(blob)

And on the C# side, I got it to work with

Stream ms = Console.OpenStandardInput();

OTHER TIPS

One possibility would be to use something like Python for .NET, which provides interop directly between C# and (standard, C) Python.

Depending on what your Python routines need to do, IronPython can also be a good option, as this is directly consumable and usable from within C#.

Both of these options avoid trying to communicate through the command line, as you have direct access to the Python objects from .NET, and vice versa.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top