简体   繁体   中英

How to pass password to another program using python

I am using Python to call another application. I have tried with subprocess.call() and subprocess.popen() mathod with stdin and stdout parameter to run another application. I am able to open new new program but application require password to login. please help me, How to pass password to window application.

It should work like this:

import subprocess

args = [app_path, add_arguments]
process = subprocess.Popen(args, subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
process.stdin.write(bytes(yourpwd + "\n"))
output = process.communicate()

While opening your application with Popen, you connect the standard input stream of this application with a Pipe that is managed by subprocess. Afterwards you can write directly to this stream, just like your a typing in a console window. However, you have to send everything with process.communicate() , since it finalizes your input and waits for the results of the program. Be aware, your application closes after calling communicate .

I have not found a workaround to avoid process.communicate . So if you like to have some kind of interactivity, you have to try something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM