unable to provide password to a process with subprocess [python]

前端 未结 2 922
小鲜肉
小鲜肉 2020-12-12 05:47

I\'m using subprocess to run a script from within python. I tried this

option 1

password = getpass.getpass()
from subprocess import Popen, PIPE, che         


        
2条回答
  •  爱一瞬间的悲伤
    2020-12-12 06:14

    You should be passing the password as a value to the communicate() function instead of stdin.write(), like so:

    from getpass import getpass
    from subprocess import Popen, PIPE
    
    password = getpass("Please enter your password: ")
    proc = Popen("command option1 option2".split(), stdin=PIPE, stdout=PIPE)
    # Popen only accepts byte-arrays so you must encode the string
    proc.communicate(password.encode())
    

提交回复
热议问题