-
详见 在线文档/
-
官方API文档: https://www.bt.cn/api-doc.pdf
pip install bt-python-sdkgit clone https://github.com/adamzhang1987/bt-python-sdk.git
python setup.py installSDK使用环境变量进行配置。您可以通过以下两种方式设置:
-
使用
.env文件(推荐):# 在项目根目录创建 .env 文件 BT_API_KEY="your-api-key" BT_PANEL_HOST="http://localhost:8888" DEBUG=False TIMEOUT=30 VERIFY_SSL=False
-
直接设置环境变量:
export BT_API_KEY="your-api-key" export BT_PANEL_HOST="http://localhost:8888" export DEBUG=False export TIMEOUT=30 export VERIFY_SSL=False
注意:
.env文件应该添加到.gitignore中以保护您的API密钥安全。
接口初始化
BT_PANEL_HOST参数不需要安全入口,只需要填写面板的域名或IP加端口即可,如:http://192.168.1.168:8888。
# 系统状态相关接口api
>>> from pybt.api import System
>>> system_api = System() # 将自动使用环境变量
# 获取系统基础统计
>>> system_api.get_system_total()
{'memTotal': 31700,
'memFree': 18403,
'memBuffers': 1020,
'memCached': 8444,
'memRealUsed': 3833,
'cpuNum': 12,
'cpuRealUsed': 4.9,
'time': '36天',
'system': 'Ubuntu 20.04.3 LTS x86_64(Py3.7.9)',
'isuser': 0,
'isport': True,
'version': '7.7.0'}
# 获取磁盘分区信息
>>> system_api.get_disk_info()
[{'filesystem': '/dev/sda6',
'type': 'ext4',
'path': '/',
'size': ['1.1T', '23G', '1005G', '3%'],
'inodes': ['72089600', '360084', '71729516', '1%']}]# 网站管理相关接口
>>> from pybt.api import Website, WebsiteBackup, Domain, Rewrite, Directory, PasswordAccess, TrafficLimit, DefaultDocument
>>> website_api = Website() # 将自动使用环境变量
# 获取网站列表
>>> website_api.get_website_list()
{'where': '',
'page': "<div><span class='Pcurrent'>1</span><span class='Pcount'>共1条</span></div>",
'data': [{'id': 5,
'name': '10.10.11.181',
'path': '/www/wwwroot/webSiteDir',
'status': '1',
'ps': '10_10_11_181',
'addtime': '2021-06-12 22:57:32',
'edate': '0000-00-00',
'backup_count': 0,
'domain': 2,
'ssl': {'issuer': 'R3',
'notAfter': '2022-03-09',
'notBefore': '2021-12-09',
'dns': ['*.*.com'],
'subject': '*.*.com',
'endtime': 73},
'php_version': '静态'}]}
# 获取PHP版本信息
>>> website_api.get_php_versions()
[{'version': '00', 'name': '纯静态'}, {'version': '56', 'name': 'PHP-56'}]
# 网站备份管理
>>> backup_api = WebsiteBackup()
>>> backup_api.get_backup_list(search=5) # 获取网站ID为5的备份列表
# 域名管理
>>> domain_api = Domain()
>>> domain_api.get_domain_list(site_id=5) # 获取网站ID为5的域名列表
# 目录和配置管理
>>> dir_api = Directory()
>>> dir_api.get_root_path(id=5) # 获取网站ID为5的根目录
# 密码访问控制
>>> pwd_api = PasswordAccess()
>>> pwd_api.set_password_access(id=5, username="admin", password="secret")
# 流量限制管理
>>> traffic_api = TrafficLimit()
>>> traffic_api.set_traffic_limit(id=5, perserver=100, perip=10, limit_rate=1024)
# 默认文档管理
>>> doc_api = DefaultDocument()
>>> doc_api.set_default_document(id=5, index="index.php,index.html")点击小三角展开查看模块方法,具体模块参数见文档目录 在线文档
System: 系统状态相关接口
get_system_total 获取系统基础统计get_disk_info 获取磁盘分区信息get_network 获取实时状态信息(CPU、内存、网络、负载)get_task_count 检查是否有安装任务update_panel 检查面板更新
Website: 基础网站管理
get_website_list 获取网站列表get_site_types 获取网站分类get_php_versions 获取已安装的 PHP 版本列表create_website 创建网站delete_website 删除网站stop_website 停用网站start_website 启用网站set_expiry_date 设置网站有效期set_website_remark 修改网站备注
WebsiteBackup: 网站备份管理
get_backup_list 获取网站备份列表create_backup 创建网站备份delete_backup 删除网站备份
Domain: 域名管理
get_domain_list 获取网站域名列表add_domain 添加网站域名delete_domain 删除网站域名
Rewrite: 重写规则和配置管理
get_rewrite_list 获取可用的重写规则get_rewrite_content 获取重写规则内容save_rewrite_content 保存重写规则内容
Directory: 网站目录和运行时配置
get_root_path 获取网站根目录get_directory_config 获取目录配置toggle_cross_site 切换跨站保护toggle_access_log 切换访问日志set_root_path 设置网站根目录set_run_path 设置网站运行目录
PasswordAccess: 密码访问控制
set_password_access 设置网站密码访问close_password_access 关闭网站密码访问
TrafficLimit: 流量限制管理
get_traffic_limit 获取流量限制配置set_traffic_limit 设置流量限制配置close_traffic_limit 关闭流量限制
DefaultDocument: 默认文档管理
get_default_document 获取默认文档配置set_default_document 设置默认文档配置
BT_API_KEY="your-api-key"
BT_PANEL_HOST="http://localhost:8888"
DEBUG=False
TIMEOUT=30
VERIFY_SSL=False# 仅运行单元测试
pytest
# 运行单元测试和集成测试
pytest --run-integration
# 仅运行集成测试
pytest -m integration --run-integrationGood Luck! ⭐
Powered by bt APIs.