Python-var = var = var值

时间:2018-07-05 04:53:58

标签: python python-requests

您能帮我吗?

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

感谢您的帮助。 谢谢

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])