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
PlexAutoSkip - Automatically skip content in Plex

PlexAutoSkip Automatically skip tagged content in Plex A background python scrip

Michael Higgins 97 Dec 21, 2022
A discord bot with information and template tracking for pxls.space.

pyCharity A discord bot with information and template tracking for pxls.space. Inspired by Mikarific's Charity bot. Try out the beta version on your s

1 Dec 03, 2021
A bot which is a ghost and you can make friends with it

This is a bot which is a ghost and you can make friends with it. It will haunt your friends. Explore and test the bot in replit !

Siwan SR 0 Oct 06, 2022
tgEasy's Official Assistant Bot and Example Bot

tgEasy Assistant The assistant bot that helps people with tgEasy directly on Telegram. This repository contains the source code of @tgEasyRobot and th

Divide Projects™ 4 Dec 26, 2022
A Discord Bot for the Pygame Community Server

PygameCommunityBot The Pygame Community Discord bot The bot is capable of doing a lot of stuff, the command prefix is pg!. For help on all the bot com

PygameCommunityDiscord 23 Nov 30, 2022
This is Telegram Files Store Bot by @AbirHasan2005

PyroFilesStoreBot This is Telegram Parmanent Files Store Bot by @AbirHasan2005. Language: Python3 Library: Pyrogram Features: In PM Just Forward or Se

Abir Hasan 168 Dec 19, 2022
A way to export your saved reddit posts to a Notion table.

reddit-saved-to-notion A way to export your saved reddit posts and comments to a Notion table.Uses notion-sdk-py and praw for interacting with Notion

19 Sep 12, 2022
Git Plan - a better workflow for git

git plan A better workflow for git. Git plan inverts the git workflow so that you can write your commit message first, before you start writing code.

Rory Byrne 178 Dec 11, 2022
An advanced telegram country information finder bot.

Country-Info-Bot-V2 An advanced telegram country information finder bot Made with Python3 (C) @FayasNoushad Copyright permission under MIT License Lic

Fayas Noushad 16 Nov 12, 2022
Discord Online Account Forever

💠 Discord-Online-Account-Forever Discord Online Account Forever 📸 Tutorial Token Discord NEVER SHARE YOUR TOKEN Installation Replit 🧿 Replit : Here

nimaisox 2 Nov 28, 2021
Telegram Link Wayback Bot. This bot archives a web page thrown at itself with wayback Machine (Archive.org).

Telegram Link Wayback Bot. This bot archives a web page thrown at itself with wayback Machine (Archive.org).

Hüzünlü Artemis [HuzunluArtemis] 11 Feb 18, 2022
API to retrieve the number of grades on the OGE website (Website listing the grades of students) to know if a new grade is available. If a new grade has been entered, the program sends a notification e-mail with the subject.

OGE-ESIREM-API Introduction API to retrieve the number of grades on the OGE website (Website listing the grades of students) to know if a new grade is

Benjamin Milhet 5 Apr 27, 2022
A ideia é fornecer uma base ampla de questões do ENEM como uma api REST

base10 "A ideia é fornecer uma base ampla de questões do ENEM como uma api REST" TODO Documentar a api com apifairy Criar testes Criar crawler para si

Wadson Garbes 4 Apr 24, 2022
Personal Discord Python Bot based on Discord.py

Personal Discord bot using the discord.py library by Rapptz

2 Dec 14, 2022
Twitch Points Miner for multiple accounts with Discord logging

Twitch Points Miner for multiple accounts with Discord logging Creator of the Twitch Miner -- PLEASE NOTE THIS IS PROBABLY BANNABLE -- Made on python

8 Apr 27, 2022
A Telegram bot that can stream Telegram files to users over HTTP.

T.ME_FILE_TO_LINK Hi iam a file to link bot....best Bot telegram Telegram File To Link Generation Bot A Telegram bot that can stream Telegram files to

1 Oct 24, 2021
Yok bentar lagi update Premium :( DI FOLLOW YA GUYS

SIMBF + PREMIUM PRINTAH PENGINSTALAN ON TERMUX $ pkg update && upgrade $ termux-setup-storage $ pkg install python $ pkg install git $ pip install bs4

Jeeck 21 Jan 14, 2022
Wanna play on the Overwatch NA servers?

OverwatchRegionSwapper-NA- Wanna play on the Overwatch NA servers? Use at you own risk. LIST OF IPs EU Netherlands: From: 5.42.168.0 to: 5.42.175.255

1 Jun 08, 2022
Python client library for Postmark API

Postmarker Python client library for Postmark API. Gitter: https://gitter.im/Stranger6667/postmarker Installation Postmarker can be obtained with pip:

Dmitry Dygalo 109 Dec 13, 2022
Grade Notifyer Bot

A bot that automatically crawl the submission platform of montefiore to notify the student when a project has been graded.

Julien Gustin 2 Jun 02, 2022