Files
soul-yongping/scripts/demo.py

147 lines
5.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#coding: utf-8
# +-------------------------------------------------------------------
# | 宝塔Linux面板
# +-------------------------------------------------------------------
# | Copyright (c) 2015-2099 宝塔软件(http://bt.cn) All rights reserved.
# +-------------------------------------------------------------------
# | Author: 黄文良 <2879625666@qq.com>
# +-------------------------------------------------------------------
#------------------------------
# API-Demo of Python
#------------------------------
import time,hashlib,sys,os,json
# 从 deploy_baota_pure_api.py 提取的配置
class bt_api:
__BT_KEY = 'hsAWqFSi0GOCrunhmYdkxy92tBXfqYjd'
__BT_PANEL = 'https://42.194.232.22:9988'
#如果希望多台面板,可以在实例化对象时,将面板地址与密钥传入
def __init__(self,bt_panel = None,bt_key = None):
if bt_panel:
self.__BT_PANEL = bt_panel
self.__BT_KEY = bt_key
#取面板日志
def get_logs(self):
#拼接URL地址
url = self.__BT_PANEL + '/data?action=getData'
#准备POST数据
p_data = self.__get_key_data() #取签名
p_data['table'] = 'logs'
p_data['limit'] = 10
p_data['tojs'] = 'test'
#请求面板接口
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';
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
# 禁用SSL证书验证用于HTTPS连接
ssl._create_default_https_context = ssl._create_unverified_context
cookie_obj = http.cookiejar.MozillaCookieJar(cookie_file)
# 加载已保存的cookie如果存在
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对象
print("=" * 50)
print("宝塔面板 API 测试")
print("=" * 50)
# 创建实例以访问配置
my_api = bt_api()
panel_url = 'https://42.194.232.22:9988'
api_key = 'hsAWqFSi0GOCrunhmYdkxy92tBXfqYjd'
print("面板地址:", panel_url)
print("API密钥:", api_key[:10] + "..." + api_key[-5:])
print("=" * 50)
try:
#调用get_logs方法
print("\n正在获取面板日志...")
r_data = my_api.get_logs()
#打印响应数据
print("\n响应结果:")
print(json.dumps(r_data, indent=2, ensure_ascii=False))
# 检查响应状态
if isinstance(r_data, dict):
if r_data.get('status') is True:
print("\n[成功] 连接成功!")
elif 'data' in r_data:
print("\n[成功] 已获取日志数据,共 %d 条记录" % len(r_data.get('data', [])))
else:
print("\n[失败] 连接失败:", r_data.get('msg', '未知错误'))
else:
print("\n响应格式:", type(r_data))
except Exception as e:
print("\n[错误] 请求异常:", str(e))
import traceback
traceback.print_exc()