vcenter通過python,nodejs實現https客戶端權限(auth)驗證(六)

Python版本:

import requests
import json

session = requests.post("https://172.17.xx.xx/rest/com/vmware/cis/session", auth=('[email protected]','xxxxx!@#'), verify=False)
session_id = session.json()['value']

response = requests.get('https://172.17.150.15/rest/vcenter/host', verify=False, headers={
	'vmware-api-session-id':session_id
	})

# print(response.json)
# print(u'response.text= %s' %str(response.text))

json_data = json.loads(response.text)

# print(json_data['value'][len(json_data['value']) -1])
print(json_data)

nodejs版本:

const https = require('https');

const options = {
    hostname: 'vc22.xx.xx',
    port: 443,
    path: '/rest/com/vmware/cis/session',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
    },
    auth: '[email protected]:xxxxx',
    rejectUnauthorized: false
};

const req = https.request(options, (res) => {
    console.log('statusCode:', res.statusCode);
    console.log('headers:', res.headers);

    res.on('data', (d) => {
        process.stdout.write(d);
    });
});

req.on('error', (e) => {
    console.error(e);
});
req.end();

 返回的結果示例:

statusCode: 200
headers: {
  date: 'Thu, 11 Jun 2020 03:16:00 GMT',
  'set-cookie': [
    'vmware-api-session-id=3928916724977c87fa93b0058cdf6f4b;Path=/rest;Secure;HttpOnly'
  ],
  expires: 'Thu, 01 Jan 1970 00:00:00 GMT',
  'content-type': 'application/json',
  connection: 'close'
}
{"value":"3928916724977c87fa93b0058cdf6f4b"}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章