forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe.py
More file actions
34 lines (27 loc) · 893 Bytes
/
pipe.py
File metadata and controls
34 lines (27 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Copyright The IETF Trust 2010-2020, All Rights Reserved
# -*- coding: utf-8 -*-
# Simplified interface to os.popen3()
def pipe(cmd, str=None):
from subprocess import Popen, PIPE
bufsize = 4096
MAX = 65536*16
if str and len(str) > 4096: # XXX: Hardcoded Linux 2.4, 2.6 pipe buffer size
bufsize = len(str)
pipe = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, bufsize=bufsize, shell=True)
if not str is None:
pipe.stdin.write(str)
pipe.stdin.close()
out = b""
err = b""
while True:
str = pipe.stdout.read()
if str:
out += str
code = pipe.poll()
if code != None:
err = pipe.stderr.read()
break
if len(out) >= MAX:
err = "Output exceeds %s bytes and has been truncated" % MAX
break
return (code, out, err)