There are plenty of questions with this Python error; I've seen:
- Python struct.unpack errors with TypeError: a bytes-like object is required, not 'str'
- struct unpack (Type Error: a byte like object is required, not 'str"), Unpack a List?
- Error "TypeError: a bytes-like object is required, not 'int'"
... unfortunately, I still don't get what exactly is wrong in my example.
So, I want to get Unix timestamp as uint32 from python, then pipe this "byte string" to another python instance, which will then decode it back; this is how far I got:
$ python3 -c 'import sys,time,struct;sys.stdout.buffer.write(struct.pack(">I", int(time.time())))' | \
python3 -c 'import sys,struct; sys.stdout.buffer.write( struct.unpack(">I", bytes(sys.stdin.buffer.read())) )'
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: a bytes-like object is required, not 'tuple'
The first command seems fine - 4 bytes are output as expected:
$ python3 -c 'import sys,time,struct;sys.stdout.buffer.write(struct.pack(">I", int(time.time())))' | hexdump -C
00000000 62 2d f9 8d |b-..|
00000004
So the error must be with the second command:
python3 -c "\
import sys,struct;\
sys.stdout.buffer.write( struct.unpack('>I', bytes(sys.stdin.buffer.read())) );\
"
From Read stdin as binary I know I should use sys.stdin.buffer
to get access to "raw" bytes ... except I don't really see how/where I need to convert to "bytes".
What am I missing - and how can I get this pipeline to work?