Sam Mussmann's answer explains why what you're doing won't work.
But if you want to know how to fix it… I'm not entirely sure what you mean by "force a binary input".
How do you know when the input is done? For input
, it's done as soon as the user hits a newline, so that clearly isn't what you want.
Also, input
automatically decode
s whatever the user gives it to give you a Unicode str
. That's clearly not what you want here.
On top of that, input
will use readline
if it's available, which will cause various control characters to do line-editing stuff instead of just representing themselves.
If, say, you want to read everything until EOF, that's easy:
payload = sys.stdin.buffer.read()
… but note that in an interactive app, "until EOF" means "until the user hits ^D on a line by itself (Unix) or ^Z under some not-clearly-documented circumstances" (Windows)", which may not be very useful. It works great for shell input redirection, but you're talking about pasting into an interactive app.
If you want to read some fixed size, like 512 bytes, that's easy too, and doesn't have any such problems:
payload = sys.stdin.buffer.read(512)
It's hard to imagine anything else you could mean that makes sense. For example, it's pretty easy to read multiple lines up until a blank line, but binary data could easily have a blank line in the middle of it. Or anything else you can think of to use.