how to pass variable in pywinauto.Application().start(cmd_line='')

谁说胖子不能爱 提交于 2021-02-11 14:21:27

问题


I have this code below:

from pywinauto.application import Application
'''user-defined package user_input1'''
from user_input1 import Get_USER,Get_TOKEN
import time

server='host.domain.com'
cwd=r'C:\Program Files (x86)\PuTTY'
user = 'uname'
'''user = Get_USER()'''
password = Get_TOKEN()
app = Application().start(cmd_line='putty -ssh uname@host.domain.com')
putty = app.PuTTY
putty.wait('ready')
time.sleep(1)
putty.type_keys(password)
putty.type_keys("{ENTER}")
time.sleep(1)
putty.type_keys("export TMOUT=0")
putty.type_keys("{ENTER}")

This code works as expected. But when I change the line app = Application().start(cmd_line='putty -ssh uname@host.domain.com') to app = Application().start(cmd_line="putty -ssh user@server") i.e when I try to pass the variable names instead of the actual values, it is not able to make the connection because the user and server variables don't expand to their actual values.

How can I correctly pass the variables.


回答1:


The usual python string expansion actually worked in this case.

Here is my updated code:

from pywinauto.application import Application
'''user-defined package user_input1'''
from user_input1 import Get_USER,Get_TOKEN
import time

server='host.domain.com'
cwd=r'C:\Program Files (x86)\PuTTY'
user = Get_USER()
password = Get_TOKEN()
cmd = 'putty -ssh '+user+'@'+server
app = Application().start(cmd_line=cmd)
putty = app.PuTTY
putty.wait('ready')
time.sleep(1)
putty.type_keys(password)
putty.type_keys("{ENTER}")
time.sleep(1)
putty.type_keys("export TMOUT=0")
putty.type_keys("{ENTER}")

Works just as expected.



来源:https://stackoverflow.com/questions/64793920/how-to-pass-variable-in-pywinauto-application-startcmd-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!