python调用宝塔控制面板API,完全控制宝塔 Linux 面板的所有功能

前言

过宝塔 API,可以完全控制宝塔 Linux 面板的所有功能,包括第三方插件应用功能,事实上,在用户登录面板
后使用的所有功能也是通过相同的接口对接的,这意味着,如果你熟悉使用浏览器调试器,就可以轻松对照宝塔 Linux
面板的操作参数完成一个第三方的前端对接。

签名算法

api_sk = 接口密钥 (在面板设置页面 – API 接口中获取)

request_time = 当前请求时间的 uinx 时间戳 ( php: time() / python: time.time() )

request_token = md5(string(request_time) + md5(api_sk))

签名

参数名称 参数值 说明
request_time   当前uinx时间戳[必传]
request_token   md5(string(request_time) + md5(api_sk)) [必传]
其它参数   功能接口需要的其它参数 [可选]

注意事项

1、请统一使用 POST 方式请求 API 接口

2、为了确保请求效率,请保存 cookie,并在每次请求时附上 cookie

3、为了面板安全考虑,请务必添加 IP 白名单

4、所有响应内容统一为 Json 数据格式

Python代码示例

import time,hashlib,sys,os,json
class bt_api:
    __BT_KEY = '密钥'
    __BT_PANEL = '宝塔登录地址'

    #如果希望多台面板,可以在实例化对象时,将面板地址与密钥传入
    def __init__(self,bt_panel = None,bt_key = None):
        if bt_panel:
            self.__BT_PANEL = bt_panel
            self.__BT_KEY = bt_key


    def get_linux(self):
        url = self.__BT_PANEL + '/system?action=GetSystemTotal'
        p_data = self.__get_key_data()  # 取签名
        # 请求面板接口
        result = self.__http_post_cookie(url, p_data)
        # 解析JSON数据
        aa= json.loads(result)
        print(aa)

    #计算MD5
    def __get_md5(self,s):
        m = hashlib.md5()
        m.update(s.encode('utf-8'))
        return m.hexdigest()

    #构造带有签名的关联数组
    def __get_key_data(self):
        now_time = int(time.time())
        p_data = {
                    'request_token':self.__get_md5(str(now_time) + '' + self.__get_md5(self.__BT_KEY)),
                    'request_time':now_time
                 }
        return p_data


    #发送POST请求并保存Cookie
    #@url 被请求的URL地址(必需)
    #@data POST参数,可以是字符串或字典(必需)
    #@timeout 超时时间默认1800秒
    #return string
    def __http_post_cookie(self, url, p_data, timeout=1800):
        cookie_file = './' + self.__get_md5(self.__BT_PANEL) + '.cookie'
        with open(cookie_file, "a") as file:
            file.write("# Netscape HTTP Cookie File\n")
        
        
        import urllib.request,ssl,http.cookiejar
        cookie_obj = http.cookiejar.MozillaCookieJar(cookie_file)
        if os.path.exists(cookie_file):
            cookie_obj.load(cookie_file,ignore_discard=True,ignore_expires=True)
        handler = urllib.request.HTTPCookieProcessor(cookie_obj)
        data = urllib.parse.urlencode(p_data).encode('utf-8')
        req = urllib.request.Request(url, data)
        opener = urllib.request.build_opener(handler)
        response = opener.open(req,timeout = timeout)
        cookie_obj.save(ignore_discard=True, ignore_expires=True)
        result = response.read()
        if type(result) == bytes: result = result.decode('utf-8')
        return result



if __name__ == '__main__':
    #实例化宝塔API对象
    my_api = bt_api()

    #调用get_logs方法
    r_data = my_api.get_linux()

 

请求实例

获取面板信息

请求接口:宝塔登陆地址’/system?action=GetSystemTotal’

#获取宝塔基本信息函数
def get_linux(self):
        url = self.__BT_PANEL + '/system?action=GetSystemTotal'
        p_data = self.__get_key_data()  # 取签名
        # 请求面板接口
        result = self.__http_post_cookie(url, p_data)
        # 解析JSON数据
        data_ = json.loads(result)
        print(data_)

 

 

 

 

 

 

 

    没有回复内容