2020년 5월 26일 화요일

[python pipe example


1. pipe example


import subprocess
import time

class pipe_run():
        def __init__(self,exec_path,prompt):
                self.proc = subprocess.Popen(
                                [exec_path],
                                stdin = subprocess.PIPE,
                                stdout = subprocess.PIPE
                        )
                self.prompt = prompt

        def write(self,command):
                self.proc.stdin.write(command.encode('utf-8'))
                self.proc.stdin.flush()

        def wait(self,echo=True):
                self.result = b""
                while True:
                        self.result += self.proc.stdout.read(1)
                        if self.result.find(self.prompt.encode('utf-8')) > 0:
                                break;
                if echo: self.pipe_dump()

        def dump(self):
                print(self.result.decode('utf-8'))

        def delay(self):
                time.sleep(1)

댓글 없음:

댓글 쓰기