Proxmox VE api python调用测试

2022/1/20 14:12:57

本文主要是介绍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'))

虚拟机开机测试

在这里插入图片描述



这篇关于Proxmox VE api python调用测试的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程