How to Write Binary Data to Stdout in Python 3

How to write binary data to stdout in python 3?

A better way:

import sys
sys.stdout.buffer.write(b"some binary data")

how to write bytes to stdout in python3.3

You can use os.fdopen to reopen stdout with different mode. You can get the file number for stdout with sys.stdout.fileno().

Example:

>>> fp = os.fdopen(sys.stdout.fileno(), 'wb')
>>> fp.write(b'Hello, world')
12
>>> sys.stdout.mode
'w'
>>> fp.mode
'wb'
>>> fp.flush()
Hello, world>>>

Python 2.x - Write binary output to stdout?

Which platform are you on?

You could try this recipe if you're on Windows (the link suggests it's Windows specific anyway).

if sys.platform == "win32":
import os, msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

There are some references on the web that there would/should be a function in Python 3.1 to reopen sys.stdout in binary mode but I don't really know if there's a better alternative then the above for Python 2.x.

Python3 how to pass binary data to stdin when using subprocess.run()?

You can pass the output of one command to another by calling Popen directly.

file_cmd1 = <your dd command>
file_cmd2 = <command you want to pass dd output to>

proc1 = Popen(sh_split(file_cmd1), stdout=subprocess.PIPE)
proc2 = Popen(file_cmd2, [shell=True], stdin=proc1.stdout, stdout=subprocess.PIPE)
proc1.stdout.close()

This, as far as I know, will work just fine on a byte output from command 1.

In your case what you most like want to do is the following when you just want to pass data to the stdin of the process:

out = bytearray(b"Some data here")
p = subprocess.Popen(sh_split("dd of=/../../somefile.data bs=32"), stdin=subprocess.PIPE)
out = p.communicate(input=b''.join(out))[0]
print(out.decode())#Prints the output from the dd

Output binary data from CGI in Python 3

Use sys.stdout.flush to force the header printed before the body:

import os
import sys

if __name__ == '__main__':
with open(os.path.abspath('test.png'), 'rb') as f:
print("Content-Type: image/png\n")
sys.stdout.flush() # <---
sys.stdout.buffer.write(f.read())

Or remove print, and use sys.stdout.buffer.write only:

import os
import sys

if __name__ == '__main__':
with open(os.path.abspath('test.png'), 'rb') as f:
sys.stdout.buffer.write(b"Content-Type: image/png\n\n") # <---
sys.stdout.buffer.write(f.read())

NOTE

f.read() could cause a problem if the file is huge. To prevent that, use shutil.copyfileobj:

import os
import shutil
import sys

if __name__ == '__main__':
with open(os.path.abspath('test.png'), 'rb') as f:
sys.stdout.buffer.write(b"Content-Type: image/png\n\n")
shutil.copyfileobj(f, sys.stdout.buffer)

How to write a raw hex byte to stdout in Python 3?

The solution was to first create a bytes object:

x = bytes.fromhex('AA')

And then output this to stdout using the buffered writer

sys.stdout.buffer.write(x)

Python: how to write binary data to the stdout so that bash script can use process substitution?

Thank you for help, I found that I can fetch through S3 using pure ogg123:

ogg123 -d wav https://s3.amazonaws.com/mybucket/file.ogg -f out.wav

Pass binary data between python processes

In Python 3 you can't use print or input for binary data. They're designed for handling text (Unicode), not binary. You can use file .write and .read calls to sys.stdout.buffer and sys.stdin.buffer, which are the underlying binary buffers to stdin and stdout. You can't use sys.stdout and sys.stdin since they're for text. There's a brief Note about this at the end of the docs for sys.stdin and sys.stdout.

Here's a short demo.

In "send_bytes.py" we create a bytes string data that contains all the possible byte values and write it to sys.stdout.buffer. We pipe that output to "get_bytes.py" where we read it and check to make sure it has all the right bytes in the right places.

send_bytes.py

#! /usr/bin/env python3

''' Write some binary data to stdout '''

import sys

# Make a bytes string containing all possible byte values
data = bytes(range(256))

#Send it as binary to stdout
out = sys.stdout.buffer
out.write(data)

get_bytes.py

#! /usr/bin/env python3

''' Read some binary data from stdin '''

import sys

#Read binary data from stdin
infile = sys.stdin.buffer
newdata = infile.read()

print(newdata)

# Make a bytes string containing all possible byte values
data = bytes(range(256))

#Check that the read data is correct
print(newdata == data)

We run the programs using this command line:

$ python3 ./send_bytes0.py | python3 ./get_bytes0.py   

Here's the output printed by "get_bytes0.py"

b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'
True


Related Topics



Leave a reply



Submit