使用popen提示时如何写入stdin?

时间:2017-02-27 21:43:32

标签: python

Iam会产生一个提示输入密码的进程,如何提供密码? 我在stackoverflow中找到了以下解决方案,但似乎无法正常工作它是否提示输入密码

class Actions():

    def console(self):
        return subprocess.Popen("myprogram",
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         shell=True,
                         close_fds=True)


    def ask_password(self, passw):
        delete = self.console().communicate("%s" % (passw))
        return(delete)


def main():
    password_string = "abc123"
    print Actions().password(password_string)

打印

('','密码:')

--bash ---
>./myprogram
Password:abc123

"Welcome to console"

0 个答案:

没有答案