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

import time,hashlib,sys,os,json
class bt_api:
    __BT_KEY = 'api密匙'
    __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)
    #创建网站
    def get_xitong(self):

        url = self.__BT_PANEL + '/site?action=AddSite'
        # 准备POST数据
        p_data = self.__get_key_data()  # 取签名
        p_data['webname'] = '{"domain":"w12.hao.com","domainlist":[],"count":0}'
        p_data['path'] = '/www/wwwroot/22.com'
        p_data['type_id'] = '0'
        p_data['type'] = 'PHP'
        p_data['version'] = '73'
        p_data['port'] = '80'
        p_data['ps'] = '测试'
        p_data['ftp'] = 'false'
        p_data['sql'] = 'false'
        p_data['codeing'] = 'utf8'
        # 请求面板接口
        result = self.__http_post_cookie(url,p_data)
        # 解析JSON数据
        return json.loads(result)


    #删除网站
    def del_web(self):
        # 拼接URL地址
        url = self.__BT_PANEL + '/site?action=DeleteSite'

        # 准备POST数据
        p_data = self.__get_key_data()  # 取签名
        p_data['id'] = '26'
        p_data['webname'] = 'w1_hao_com'


        # 请求面板接口
        result = self.__http_post_cookie(url, p_data)

        # 解析JSON数据
        return json.loads(result)



    #计算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")
        if sys.version_info[0] == 2:
            #Python2
            import urllib,urllib2,ssl,cookielib

            #创建cookie对象
            cookie_obj = cookielib.MozillaCookieJar(cookie_file)

            #加载已保存的cookie
            if os.path.exists(cookie_file):cookie_obj.load(cookie_file,ignore_discard=True,ignore_expires=True)

            ssl._create_default_https_context = ssl._create_unverified_context

            data = urllib.urlencode(p_data)
            req = urllib2.Request(url, data)
            opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_obj))
            response = opener.open(req,timeout=timeout)

            #保存cookie
            cookie_obj.save(ignore_discard=True, ignore_expires=True)
            return response.read()
        else:
            #Python3
            import urllib.request,ssl,http.cookiejar
            cookie_obj = http.cookiejar.MozillaCookieJar(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()

 

 

© 版权声明
评论 抢沙发