如何在python connection.request url中使用变量

时间:2019-03-04 19:40:05

标签: python python-3.x url http.client

我正在尝试读取变量中的服务器名称(例如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)

1 个答案:

答案 0 :(得分:0)

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

第二,您不应在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)