| 35 | return f'CurlPiper[exitcode={self._exitcode}, stderr={self._stderr}, stdout={self._stdout}]' |
| 36 | |
| 37 | def start(self): |
| 38 | self.args, self.headerfile = self.env.curl_complete_args([self.url], timeout=5, options=[ |
| 39 | "-T", "-", "-X", "POST", "--trace-ascii", "%", "--trace-time" |
| 40 | ]) |
| 41 | self.args.append(self.url) |
| 42 | sys.stderr.write("starting: {0}\n".format(self.args)) |
| 43 | self.proc = subprocess.Popen(self.args, stdin=subprocess.PIPE, |
| 44 | stdout=subprocess.PIPE, |
| 45 | stderr=subprocess.PIPE, |
| 46 | bufsize=0) |
| 47 | |
| 48 | def read_output(fh, buffer): |
| 49 | while True: |
| 50 | chunk = fh.read() |
| 51 | if not chunk: |
| 52 | break |
| 53 | buffer.append(chunk.decode()) |
| 54 | |
| 55 | # collect all stdout and stderr until we are done |
| 56 | # use separate threads to not block ourself |
| 57 | self._stderr = [] |
| 58 | self._stdout = [] |
| 59 | if self.proc.stderr: |
| 60 | self.stderr_thread = Thread(target=read_output, args=(self.proc.stderr, self._stderr)) |
| 61 | self.stderr_thread.start() |
| 62 | if self.proc.stdout: |
| 63 | self.stdout_thread = Thread(target=read_output, args=(self.proc.stdout, self._stdout)) |
| 64 | self.stdout_thread.start() |
| 65 | return self.proc |
| 66 | |
| 67 | def send(self, data: str): |
| 68 | self.proc.stdin.write(data.encode()) |