如何在 pywinauto.Application().start(cmd_line='') 中传递变量 [英] how to pass variable in pywinauto.Application().start(cmd_line='')

查看:126
本文介绍了如何在 pywinauto.Application().start(cmd_line='') 中传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有这个代码:

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}")

此代码按预期工作.但是当我将行 app = Application().start(cmd_line='putty -ssh uname@host.domain.com') 更改为 app = Application().start(cmd_line="putty -ssh user@server") 即当我尝试传递变量名称而不是实际值时,它无法建立连接,因为 userserver 变量不会扩展到它们的实际值.

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.

如何正确传递变量.

推荐答案

通常的 Python 字符串扩展在这种情况下确实有效.

The usual python string expansion actually worked in this case.

这是我更新的代码:

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}")

按预期工作.

这篇关于如何在 pywinauto.Application().start(cmd_line='') 中传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆