如何在pythonconnection.requesturl中使用变量

How to use a variable in the python connection.request url

我正在尝试读取变量中的服务器名称(例如:server1)并将其传递给 url。但是,url 不接受变量,而只接受硬编码在其中的服务器名称。

有什么方法可以实现吗?

 def connect_to_BNAserver(BNAserver):
   connection = http.client.HTTPConnection(BNAserver)
   baseurl="'https://"+BNAserver+"/rest/login'"
   header = {"WSUsername": BNAUser, "WSPassword": BNAPass,"Accept": "application/vnd.brocade.networkadvisor+json;version=v1"}

   connection.request('POST',BaseUrl,headers=header)  

以下作品:

   header = {"WSUsername": BNAUser, "WSPassword": BNAPass,"Accept": "application/vnd.brocade.networkadvisor+json;version=v1"}

   connection.request('POST','https://server1/rest/login',headers=header)

第一个变量名称区分大小写。

其次,你不应该在 URL 中加上引号(你在工作案例中没有引号)。

def connect_to_BNAserver(BNAserver):
   connection = http.client.HTTPConnection(BNAserver)
   baseurl="https://"+BNAserver+"/rest/login"
   header = {"WSUsername": BNAUser, "WSPassword": BNAPass,"Accept": "application/vnd.brocade.networkadvisor+json;version=v1"}

   connection.request('POST',baseurl,headers=header)