![](/img/trans.png)
[英]Python create windows exe with a config file to pass username and password
[英]python execute exe file with script, enter username, password, etc
我想調用一個exe文件,並且已經輸入了參數/輸入數據。
cmd = dir_path + 'file.exe do something test'
p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)
這已經可以正常工作了。 如果使用exe文件執行此操作,則按Enter鍵以執行下一步,即輸入用戶名。 然后輸入密碼,等等。 現在,我不知道如何將其實現到腳本中,所有這些步驟都是在腳本中完成的。 基本上我不知道如何用python執行ENTER步驟。 感謝任何幫助。
UPDATE
現在嘗試了2種方法(在此問題中提到的所有方法,或者在stackoverflow上提及的其他方法)。
方式1>
p = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, universal_newlines=True)
# cmd represents the path of the exe file
p.stdin.write(username+"\n")
p.stdin.write(password+"\n")
p.sdin.close()
方式2
p = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, universal_newlines=True)
out,err = p.communicate("\n".join([username,password,"\n"]))
# last \n should terminate the exe file and communicate should wait for the process to exit
兩種方式在一行中為我提供相同的輸出:
用戶名密碼:
通常我希望會是:
用戶名:“由進程寫的用戶名+ \\ n”
密碼:“密碼由進程+ \\ n寫入”
該應用程序在Windows7上運行(必須),並且exe文件在命令行中運行。
因此,exe是否可能不支持way1和way2,所以不可能通過子進程編寫它,或者我犯了一個重大錯誤?
再次感謝任何幫助。
你快到了!
您可以執行以下操作:
import subprocess
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, shell=True)
p.communicate(input='\r')
如果有任何問題,您可能需要使用\\ n而不是\\ r。 \\ r是回車符,\\ n是換行符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.