diff options
author | Michael P. Soulier <msoulier@digitaltorque.ca> | 2021-05-19 12:02:04 -0400 |
---|---|---|
committer | Michael P. Soulier <msoulier@digitaltorque.ca> | 2021-05-19 12:02:04 -0400 |
commit | 627823548eda365a4bf1129e091eb252320f0954 (patch) | |
tree | aa59cdb922f6fe11a439f2d6bb1aa0ec88733469 | |
parent | fd3692188bd1c12e96b980ad147c04c1bd0fbaad (diff) | |
parent | f81af33fcda34f1f0c12c01dcc8481f04377d1d3 (diff) | |
download | tftpy-627823548eda365a4bf1129e091eb252320f0954.tar.gz |
Merge remote-tracking branch 'ulidtko/fix/binary-stdin'
Merging PR 116.
-rw-r--r-- | tftpy/TftpContexts.py | 3 | ||||
-rw-r--r-- | tftpy/compat.py | 15 |
2 files changed, 17 insertions, 1 deletions
diff --git a/tftpy/TftpContexts.py b/tftpy/TftpContexts.py index 679413e..f01509e 100644 --- a/tftpy/TftpContexts.py +++ b/tftpy/TftpContexts.py @@ -15,6 +15,7 @@ from .TftpShared import * from .TftpPacketTypes import * from .TftpPacketFactory import TftpPacketFactory from .TftpStates import * +from . import compat import socket import time import sys @@ -286,7 +287,7 @@ class TftpContextClientUpload(TftpContext): if hasattr(input, 'read'): self.fileobj = input elif input == '-': - self.fileobj = sys.stdin + self.fileobj = compat.binary_stdin() else: self.fileobj = open(input, "rb") diff --git a/tftpy/compat.py b/tftpy/compat.py new file mode 100644 index 0000000..0049396 --- /dev/null +++ b/tftpy/compat.py @@ -0,0 +1,15 @@ +import sys + +def binary_stdin(): + """ + Get a file object for reading binary bytes from stdin instead of text. + Compatible with Py2/3, POSIX & win32. + Credits: https://stackoverflow.com/a/38939320/531179 (CC BY-SA 3.0) + """ + if hasattr(sys.stdin, 'buffer'): # Py3+ + return sys.stdin.buffer + else: + if sys.platform == 'win32': + import os, msvcrt + msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) + return sys.stdin |