4
620hun
7y

Does anyone by any chance know how to pipe binary data out of Python? I digged through the first 10 pages of Google, but none of that did what I needed. I know how to pipe things between subprocesses, but not to the stdout of Python.

Comments
  • 1
    By binary I mean a PDF to be exact.
  • 1
    This is a question that I'm almost completely sure is answered in StackOverflow
  • 0
    @edwrodrig I'll have another look. I tried like 10 ways that were mentioned there, but they didn't work.
  • 0
    Well, you *can* output binary and you *can* redirect stdout...
  • 0
    @-eth I have a chain that looks like this scanner command => converter => >stdout of Python <

    I have trouble with the last part. I want to output to stdout, which would be read by PHP.
  • 0
    sys.stdout is a file-like object. You should be able to write whatever you want to it, including binary data. Also sys.stdout.flush() is a useful function
  • 0
    I GOT IT!

    pipeline = subprocess.Popen(temp_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    pipeline.stdout.close()
    pipeline.communicate()

    It was the communicate() that did the trick.
Add Comment