5

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.

4
  • why not just use python.NET? depending on what you are doing, it works pretty well... Commented Mar 4, 2014 at 19:09
  • Related: stackoverflow.com/q/7367976 Commented Mar 4, 2014 at 19:09
  • @CorleyBrigman: What is python.net? Commented Mar 4, 2014 at 19:10
  • see reed's answer below (we posted nearly at the same time), since he has a link there. it lets you access C# libraries in a way that is nearly native - you can even import a C# library in the console, and do some interactive tab completion, etc. Commented Mar 4, 2014 at 19:11

2 Answers 2

4

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();
Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

I get the same error as described here, stackoverflow.com/questions/14633695/…
make sure you match the python.NET version you are installing, with the version of python you have - mixing the two can cause weird errors like that.
@horriblyUnpythonic The versioning with python/python for .NET is typically the main issue.

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.