1 | #!/usr/bin/env python2
|
2 | from __future__ import print_function
|
3 | """
|
4 | read_from_fd.py
|
5 | """
|
6 |
|
7 | import os
|
8 | import sys
|
9 |
|
10 |
|
11 | def main(argv):
|
12 | # Note: the shell has to open this fd
|
13 | for arg in sys.argv[1:]:
|
14 | fd = int(arg)
|
15 | #print 'reading from', fd
|
16 | try:
|
17 | in_str = os.read(fd, 1024)
|
18 | except OSError as e:
|
19 | print('FATAL: Error reading from fd %d: %s' % (fd, e),
|
20 | file=sys.stderr)
|
21 | sys.exit(1)
|
22 |
|
23 | s = '%d: ' % fd
|
24 | sys.stdout.write(s.encode('utf-8'))
|
25 | sys.stdout.write(in_str) # write binary data to stdout
|
26 |
|
27 |
|
28 | if __name__ == '__main__':
|
29 | main(sys.argv)
|