1

There are plenty of questions with this Python error; I've seen:

... 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?

0

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.