Pybt: a BaoTa panel python sdk

Overview

About

Pybt is a BaoTa panel python sdk.

Pybt 是一个宝塔面板API的Python版本sdk封装库。

公司很多服务器都装了宝塔面板,通过宝塔来部署、安装、维护一些服务,服务器的数量上以后,导致了维护的不方便,这个时候就想使用宝塔提供的API来开发一个运维平台,通过平台来统一监听服务器状态和执行一些简单运维操作。网上找了一下,没有特别满意的Python封装,干脆自己撸了一套。

感谢

编写的时候参考了两位老哥的代码

Installation

pip install bt-python-sdk

or

git clone https://github.com/adamzhang1987/bt-python-sdk.git
python setup.py install

Examples

1.首先需要在 面板设置-API接口 中打开API接口,获取 接口秘钥

2.开启API后,必需在IP白名单列表中的IP才能访问面板API接口。

3.如需本机调用面板API密钥,请添加"127.0.0.1"和本机IP至IP白名单。

接口初始化YOUR_PANEL_ADDRESS参数不需要安全入口,只需要填写面板的域名或IP加端口即可,如: http://192.168.1.168:8888

# 系统状态相关接口api
>>> from pybt.system import System
>>> system_api = System(YOUR_PANEL_ADDRESS, YOUR_API_KEY)

# 获取系统基础统计
>>> 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.sites import Sites
>>> sites_api = Sites(YOUR_PANEL_ADDRESS, YOUR_API_KEY)

# 获取网站列表
>>> sites_api.websites()

{'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版本信息
>>> sites_api.get_php_version()

[{'version': '00', 'name': '纯静态'}, {'version': '56', 'name': 'PHP-56'}]

# 获取网站SSL详情, YOUR_SITES_NAME通过websites接口获取
>>> sites_api.get_ssl(YOUR_SITES_NAME)

{'status': True,
 'oid': -1,
 'domain': [{'name': '10.10.11.181'}, {'name': '127.0.0.1'}],
 'key': YOUR_KEY,
 'csr': YOUR_CSR,
 'type': 1,
 'httpTohttps': False,
 'cert_data': {'subject': '*.*.com',
  'notAfter': '2022-03-09',
  'notBefore': '2021-12-09',
  'issuer': "Let's Encrypt",
  'dns': ['*.*.com']},
 'email': '[email protected]',
 'index': '142e5275a456ecd7bf32bda98528375c',
 'auth_type': 'http'}
# FTP管理相关接口
>>> from pybt.ftp import Ftp
>>> ftp_api = Ftp(YOUR_PANEL_ADDRESS, YOUR_API_KEY)
# 获取FTP信息列表
>>> ftp_api.web_ftp_list()

{'where': '',
 'page': "<div><span class='Pcurrent'>1</span><span class='Pcount'>共1条</span></div>",
 'data': [{'id': 1,
   'pid': 0,
   'name': 'web_user',
   'password': 'web_user_password',
   'status': '1',
   'ps': 'web_user',
   'addtime': '2021-10-25 10:48:35',
   'path': '/www/wwwroot/web_user'}]}
# 数据库管理
>>> from pybt.dbm import DBM
>>> dbm_api = DBM(YOUR_PANEL_ADDRESS, YOUR_API_KEY)
# 获取数据库信息列表
>>> dbm_api.web_db_list()

{'where': '',
 'page': "<div><span class='Pcurrent'>1</span><span class='Pcount'>共1条</span></div>",
 'data': [{'id': 1,
   'pid': 0,
   'name': 'test_site_db',
   'username': 'test_site_db',
   'password': 'test_site_db_password',
   'accept': '127.0.0.1',
   'ps': 'test_site_db',
   'addtime': '2021-10-25 10:53:15',
   'backup_count': 0}]}
# 插件管理
>>> from pybt.plugin import Plugin
>>> plugin_api = Plugin((YOUR_PANEL_ADDRESS, YOUR_API_KEY)
# 宝塔一键部署执行
>>> plugin_api.setup_package(dname, site_name, php_version)

功能模块

点击小三角展开查看模块方法,具体模块参数见文档目录 docs/

System: 系统状态相关接口

  • get_system_total 获取系统基础统计
  • get_disk_info 获取磁盘分区信息
  • get_net_work 获取实时状态信息(CPU、内存、网络、负载)
  • get_task_count 检查是否有安装任务
  • update_panel 检查面板更新
Sites: 网站管理相关接口

  • websites 获取网站列表
  • webtypes 获取网站分类
  • get_site_id 获取指定站点ID若站点不存在则返回-1
  • get_php_version 获取已安装的 PHP 版本列表
  • get_site_php_version 获取指定网站运行的PHP版本
  • set_php_version 修改指定网站的PHP版本
  • get_type_id 获取分类ID若分类不存在则返回0
  • set_has_pwd 开启并设置网站密码访问
  • close_has_pwd 关闭网站密码访问
  • get_dir_user_ini 获取网站几项开关(防跨站、日志、密码访问)
  • web_add_site 创建网站
  • web_delete_site 删除网站
  • web_site_stop 停用网站
  • web_site_start 启用网站
  • web_set_end_date 设置网站有效期
  • web_set_ps 修改网站备注
  • web_backup_list 获取网站备份列表
  • web_to_backup 创建网站备份
  • web_del_backup 删除网站备份
  • web_domain_list 获取网站域名列表
  • get_dir_binding 获取网站域名绑定二级目录信息
  • add_dir_binding 添加网站子目录域名
  • del_dir_binding 删除网站绑定子目录
  • get_dir_rewrite 获取网站子目录伪静态规则
  • web_add_domain 添加网站域名
  • web_del_domain 删除网站域名
  • get_site_logs 获取网站日志
  • get_security 获取网站盗链状态及规则信息
  • set_security 设置网站盗链状态及规则信息
  • get_ssl 获取SSL状态及证书详情
  • http_to_https 强制HTTPS
  • close_to_https 关闭强制HTTPS
  • set_ssl 设置SSL证书
  • close_ssl_conf 关闭SSL
  • web_get_index 获取网站默认文件
  • web_set_index 设置网站默认文件
  • get_limit_net 获取网站流量限制信息
  • set_limit_net 设置网站流量限制信息
  • close_limit_net 关闭网站流量限制
  • get_301_status 获取网站301重定向信息
  • set_301_status 设置网站301重定向信息
  • get_rewrite_list 获取可选的预定义伪静态列表
  • get_file_body 获取指定预定义伪静态规则内容(获取文件内容)
  • save_file_body 保存伪静态规则内容(保存文件内容)
  • get_proxy_list 获取网站反代信息及状态
  • create_proxy 添加网站反代信息
  • modify_proxy 修改网站反代信息
Ftp: Ftp管理

  • web_ftp_list 获取FTP信息列表
  • set_user_password 修改FTP账号密码
  • get_ftp_id 根据Ftp_Username获取FTPID
  • set_status 启用/禁用FTP
DBM: 数据库管理

  • web_db_list 获取SQL信息列表
  • get_db_id 修改SQL账号密码
  • res_database_pass 根据数据库名获取SQLID
  • db_to_backup 创建sql备份
  • db_del_backup 删除sql备份
Plugin: 插件管理

  • deployment 宝塔一键部署列表
  • setup_package 部署任务

Documentation

详见 docs/

Testing

执行单元测试前,请先在 tests 目录下新建一个 config.py 的文件,文件内容如下:

# config.py
CONFIG = {
    "panel_address": YOUR_PANEL_ADDRESS,
    "api_key": YOUR_API_KEY
}

然后执行:

python -m unittest

或者

pytest

祝您好运!

TODO

  • 完善测试用例
  • 完善文档
  • 完善exapmles

Powered by bt APIs.

You might also like...
Python SDK for Facebook's Graph API

Facebook Python SDK This client library is designed to support the Facebook Graph API and the official Facebook JavaScript SDK, which is the canonical

Box SDK for Python

Box Python SDK Installing Getting Started Authorization Server-to-Server Auth with JWT Traditional 3-legged OAuth2 Other Auth Options Usage Documentat

The Official Dropbox API V2 SDK for Python
The Official Dropbox API V2 SDK for Python

The offical Dropbox SDK for Python. Documentation can be found on Read The Docs. Installation Create an app via the Developer Console. Install via pip

Evernote SDK for Python

Evernote SDK for Python Evernote API version 1.28 This SDK is intended for use with Python 2.X For Evernote's beta Python 3 SDK see https://github.com

Python SDK for IEX Cloud
Python SDK for IEX Cloud

iexfinance Python SDK for IEX Cloud. Architecture mirrors that of the IEX Cloud API (and its documentation). An easy-to-use toolkit to obtain data for

Unofficial Medium Python Flask API and SDK
Unofficial Medium Python Flask API and SDK

PyMedium - Unofficial Medium API PyMedium is an unofficial Medium API written in python flask. It provides developers to access to user, post list and

The Python SDK for the Rackspace Cloud

pyrax Python SDK for OpenStack/Rackspace APIs DEPRECATED: Pyrax is no longer being developed or supported. See openstacksdk and the rackspacesdk plugi

:snake: Python SDK to query Scaleway APIs.

Scaleway SDK Python SDK to query Scaleway's APIs. Stable release: Development: Installation The package is available on pip. To install it in a virtua

Skyscanner Python SDK

Skyscanner Python SDK Important As of May 1st, 2020, the project is deprecated and no longer maintained. The latest update in v1.1.5 includes changing

Owner
Adam Zhang
Python / iOS / C++ Big data / machine learning / deep learning Flask backend
Adam Zhang
Busty - A bot for the Busty Discord server

Busty Discord bot used for the Busty server. Install You'll need at least Python

Andrew Morgan 7 Dec 05, 2022
Provide fine-grained push access to GitHub from a JupyterHub

github-app-user-auth Provide fine-grained push access to GitHub from a JupyterHub. Goals Allow users on a JupyterHub to grant push access to only spec

Yuvi Panda 20 Sep 13, 2022
Open API to list Viet Nam administrative divisions

Viet Nam province API Homepage: https://provinces.open-api.vn This is online tool to let my VietnamProvinces library reach more users. VietnamProvince

Nguyễn Hồng Quân 52 Dec 05, 2022
Simple Telegram Bot to extract various types of archives from a telegram file or a direct link

Unzipper Bot A Telegram Bot to Extract Various Types Of Archives Features Extract various types of archives like rar, zip, tar, 7z, tar.xz etc. Passwo

I'm Not A Bot #Left_TG 93 Dec 27, 2022
Unofficial GoPro API Library for Python - connect to GoPro via WiFi.

GoPro API for Python Unofficial GoPro API Library for Python - connect to GoPro cameras via WiFi. Compatibility: HERO3 HERO3+ HERO4 (including HERO Se

Konrad Iturbe 1.3k Jan 01, 2023
This Python script will automate the process of uploading your project to GitHub.

ProjectToGithub This Python script will help you to upload your project to Github without having to type in any commands !!! Quick Start guide First C

Imira Randeniya 1 Sep 11, 2022
Picot - A discord bot made to fetch images from Pexels and unsplash API and provide raw images directly in channels

Picot A discord bot made to fetch images from Pexels and unsplash API and provid

Ayush Chandwani 5 Jan 12, 2022
Python based league of legends orbwalker

League of Legends Orbwalker Usage Install python3 Create a python3 venv Install the requirements pip install -r requirements.txt Get in game and run m

Inusha 43 Dec 12, 2022
Telegram vc userbot

Telegram Vc Userbot Available Commands /ping :- To check whether userbot is up or not /joinvc :- To join vc /leavevc :- To leave vc /join_group :- To

NandyDark 7 Nov 18, 2022
Auto like & auto followers facebook

Auto like & auto followers facebook

Fahmi Dev 23 Dec 08, 2022
Unlimited Filter Bot

Unlimited Filter Bot The Orginel Owner Of This Repo Is ㅤㅤㅤㅤㅤㅤㅤ ㅤㅤㅤㅤㅤㅤㅤ An advanced Filter Bot with nearly unlimitted filters! Features Nearly unlimite

4 Aug 02, 2022
Minimal API for the COVID Booking System of the Offices at the UniPD Math Dep

Simple and easy to use python BOT for the COVID registration booking system of the math department @ unipd (torre archimede). This API creates an interface with the official website, with more useful

Guglielmo Camporese 4 Dec 24, 2021
WikipediaBot from mohirdev.uz

wiki-bot WikipediaBot from mohirdev.uz Requirements wikipedia aiogram Installing wiki/aiogram pip install wikipedia pip install aiogram

Muhammad Ali 5 Sep 28, 2022
A bot that updates about the most subscribed artist' channels on YouTube

A bot that updates about the most subscribed artist' channels on YouTube. A weekly top chart report is provided every Monday. It posts updates on Twitter

Marco Fantauzzo 5 Dec 14, 2022
A discord bot thet lets you play Space invaders.

space_Invaders A discord bot thet lets you play Space invaders. It is my first discord bot... so please give any suggestions to improve it :] Commands

2 Dec 30, 2021
Slack bot for monitoring your Metaflow flows!

Metaflowbot - Slack Bot for your Metaflow flows! Metaflowbot makes it fun and easy to monitor your Metaflow runs, past and present. Imagine starting a

Outerbounds 21 Dec 07, 2022
BSDotPy, A module to get a bombsquad player's account data.

BSDotPy BSDotPy, A module to get a bombsquad player's account data from bombsquad's servers. Badges Provided By: shields.io Acknowledgements Issues Pu

Rudransh Joshi 3 Feb 17, 2022
Easy & powerful bot to check if your all Telegram bots are working or not

Easy & powerful bot to check if your all Telegram bots are working or not. This bot status bot updates every 105 minutes & runs for 24x7 hours.

35 Dec 30, 2022
Binance leverage futures Hook

Simple binance futures Attention Just use leverage. The fee difference between futures and spot is not considered. For example, funding rate, etc. Onl

Adriance 26 Aug 27, 2022
Jika ada pertanyaan lebih lanjut, hubungi kontak dibawah ini. Terimakasih...

⚡ Lynx Userbot ⚡ Userbot Used for Fun on Telegram, and for Maintianing Your Group. This is a Repo Lynx-Userbot. This is Repo was Created by Axel From

29 Aug 30, 2021