您能帮我吗?
AdminServer = "putty.exe -ssh 1.1.1.1"
Server1 = "putty.exe -ssh 2.2.2.2"
Server = requests.get('example.com')
#this returns the server name (AdminServer or Server1...)
subprocess.Popen(Server)
当我运行它时,我得到:
WindowsError: [Error 2] The system cannot find the file specified
可能是因为subprocess.Popen
试图打开命令AdminServer
(不是命令)而不是其值(putty.exe -ssh 1.1.1.1
)
感谢您的帮助。 谢谢
答案 0 :(得分:1)
使用字典,而不是变量。如果您有变量名,则获取变量内容的方法是evil
,几乎总是有一种更好的方法。在这种情况下:
server_connection_commands = {
"AdminServer": "putty.exe -ssh 1.1.1.1",
"Server1": "putty.exe -ssh 2.2.2.2"
}
server = requests.get('http://example.com').text
subprocess.Popen(server_connection_commands[server])