编程语言
首页 > 编程语言> > Proxmox VE api python调用测试

Proxmox VE api python调用测试

作者:互联网

官方api文档:
https://pve.proxmox.com/pve-docs/api-viewer

# -*- coding:utf-8 -*-
'''
@Author: XGL
@File: pvehttp.py
@CreateTime: 2022/1/20 9:43
'''

import requests
from requests.cookies import cookiejar_from_dict
from time import sleep
import urllib3

urllib3.disable_warnings()

class Pvehttp():
    def __init__(self, host, username, password):
        self.host = host
        self.username = username
        self.password = password
        self.base_url = 'https://' + host + ':8006/'
        self.header = {'content-type': 'application/json'}
        self.request_data = {
            'username': self.username,
            'password': self.password,
            'realm': 'pam'
        }
        self.csrftoken = self.baseauth().get('data').get('CSRFPreventionToken')
        self.ticket = self.baseauth().get('data').get('ticket')
        self.cookies = cookiejar_from_dict({"PVEAuthCookie": self.ticket})
        self.post_header = {
            'Cookie': 'PVEAuthCookie={}'.format(self.ticket),
            'CSRFPreventionToken': self.csrftoken,
        }

    def baseauth(self):
        url = self.base_url + 'api2/json/access/ticket'
        response = requests.post(url, headers=self.header, json=self.request_data, verify=False).json()
        return response

    def req(self, method, apiurl, **kwargs):
        url = self.base_url + apiurl
        try:
            if method == 'get':
                response = requests.get(url, **kwargs)
                return response.json()
            elif method == 'post':
                response = requests.post(url, **kwargs)
                return response.json()
        except Exception as f:
            return f


    def getstatus(self, apiurl):
        return self.req('get', apiurl, cookies=self.cookies, verify=False)

    def poststatus(self, apiurl):
        return self.req('post',apiurl, headers=self.post_header, verify=False)


if __name__ == '__main__':
    data = {
        'username': 'root',
        'password': 'password',
        'host': '192.168.16.36'
    }
    pve = Pvehttp(**data)
    # print(pve.getstatus('api2/json/cluster/tasks'))
    print(pve.poststatus('api2/extjs/nodes/pve/qemu/101/status/start'))
    sleep(5)
	print(pve.getstatus('api2/json/nodes/pve/qemu/101/status/current'))

虚拟机开机测试

在这里插入图片描述

标签:VE,get,python,self,url,json,pve,Proxmox,password
来源: https://blog.csdn.net/qq_39438868/article/details/122599178