├── README.md └── bt.py /README.md: -------------------------------------------------------------------------------- 1 | # BtPanel-Python-SDK 2 | * @ 作者 Abudu 3 | * @ 参考 阿良 or Youngxj(二次开发) 4 | * @ 官方API文档 https://www.bt.cn/api-doc.pdf 5 | * @ 博客 https://www.abudu.top 6 | * @ Github https://github.com/am-abudu/BtPanel-API-Python-SDK 7 | * @ 版本 0.1 8 | # 完成情况(可用方法) 9 | ## 系统状态相关接口 10 | + GetSystemTotal 获取系统基础统计 √ 11 | + GetDiskInfo 获取磁盘分区信息 √ 12 | + GetNetWork 获取实时状态信息(CPU、内存、网络、负载) √ 13 | + GetTaskCount 检查是否有安装任务 √ 14 | + UpdatePanel 检查面板更新 √ 15 | ## 网站管理相关接口 16 | + Websites 获取网站列表 √ 17 | + Webtypes 获取网站分类 √ 18 | + GetSiteID 获取指定站点ID若站点不存在则返回-1 √ 19 | + GetPHPVersion 获取已安装的 PHP 版本列表 √ 20 | + GetSitePHPVersion 获取指定网站运行的PHP版本 √ 21 | + SetPHPVersion 修改指定网站的PHP版本 √ 22 | + GetTypeID 获取分类ID若分类不存在则返回0 √ 23 | + SetHasPwd 开启并设置网站密码访问 √ 24 | + CloseHasPwd 关闭网站密码访问 √ 25 | + GetDirUserINI 获取网站几项开关(防跨站、日志、密码访问) √ 26 | + WebAddSite 创建网站 √ 27 | + WebDeleteSite 删除网站 √ 28 | + WebSiteStop 停用网站 √ 29 | + WebSiteStart 启用网站 √ 30 | + WebSetEdate 设置网站有效期 √ 31 | + WebSetPs 修改网站备注 √ 32 | + WebBackupList 获取网站备份列表 √ 33 | + WebToBackup 创建网站备份 √ 34 | + WebDelBackup 删除网站备份 √ 35 | + WebDoaminList 获取网站域名列表 √ 36 | + GetDirBinding 获取网站域名绑定二级目录信息 √ 37 | + AddDirBinding 添加网站子目录域名 √ 38 | + DelDirBinding 删除网站绑定子目录 √ 39 | + GetDirRewrite 获取网站子目录伪静态规则 √ 40 | + WebAddDomain 添加网站域名 √ 41 | + WebDelDomain 删除网站域名 √ 42 | + GetSiteLogs 获取网站日志 √ 43 | + GetSecurity 获取网站盗链状态及规则信息 √ 44 | + SetSecurity 设置网站盗链状态及规则信息 √ 45 | + GetSSL 获取SSL状态及证书详情 √ 46 | + HttpToHttps 强制HTTPS √ 47 | + CloseToHttps 关闭强制HTTPS √ 48 | + SetSSL 设置SSL证书 √ 49 | + CloseSSLConf 关闭SSL √ 50 | + WebGetIndex 获取网站默认文件 √ 51 | + WebSetIndex 设置网站默认文件 √ 52 | + GetLimitNet 获取网站流量限制信息 √ 53 | + SetLimitNet 设置网站流量限制信息 √ 54 | + CloseLimitNet 关闭网站流量限制 √ 55 | + Get301Status 获取网站301重定向信息 √ 56 | + Set301Status 设置网站301重定向信息 √ 57 | + GetRewriteList 获取可选的预定义伪静态列表 √ 58 | + GetFileBody 获取指定预定义伪静态规则内容(获取文件内容) √ 59 | + SaveFileBody 保存伪静态规则内容(保存文件内容) √ 60 | + GetProxyList 获取网站反代信息及状态 √ 61 | + CreateProxy 添加网站反代信息 √ 62 | + ModifyProxy 修改网站反代信息 √ 63 | ## Ftp管理 64 | + WebFtpList 获取FTP信息列表 √ 65 | + SetUserPassword 修改FTP账号密码 √ 66 | + GetFTPID 根据Ftp_Username获取FTPID √ 67 | + SetStatus 启用/禁用FTP √ 68 | ## Sql管理 69 | + WebSqlList 获取SQL信息列表 √ 70 | + ResDatabasePass 修改SQL账号密码 √ 71 | + GetSQLID 根据数据库名获取SQLID √ 72 | + SQLToBackup 创建sql备份 √ 73 | + SQLDelBackup 删除sql备份 √ 74 | ## 插件管理 75 | + deployment 宝塔一键部署列表 √ 76 | + SetupPackage 部署任务 √ 77 | -------------------------------------------------------------------------------- /bt.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | _ _ 4 | /\ | | | | 5 | / \ | |__ _ _ __| |_ _ 6 | / /\ \ | '_ \| | | |/ _` | | | | 7 | / ____ \| |_) | |_| | (_| | |_| | 8 | /_/ \_\_.__/ \__,_|\__,_|\__,_| 9 | 10 | 11 | * 宝塔面板站点操作Python库 12 | * @ 作者 Abudu 13 | * @ 参考 阿良 or Youngxj(二次开发) 14 | * @ 官方API文档 https://www.bt.cn/api-doc.pdf 15 | * @ 博客 https://www.abudu.top 16 | * @ Github https://github.com/am-abudu/BtPanel-API-Python-SDK 17 | * @ 版本 0.1 18 | """ 19 | 20 | """ 完成情况 21 | 22 | 系统状态相关接口 ---163 23 | GetSystemTotal 获取系统基础统计 √ 24 | GetDiskInfo 获取磁盘分区信息 √ 25 | GetNetWork 获取实时状态信息(CPU、内存、网络、负载) √ 26 | GetTaskCount 检查是否有安装任务 √ 27 | UpdatePanel 检查面板更新 √ 28 | 网站管理相关接口 ---184 29 | Websites 获取网站列表 √ 30 | Webtypes 获取网站分类 √ 31 | GetSiteID 获取指定站点ID若站点不存在则返回-1 √ 32 | GetPHPVersion 获取已安装的 PHP 版本列表 √ 33 | GetSitePHPVersion 获取指定网站运行的PHP版本 √ 34 | SetPHPVersion 修改指定网站的PHP版本 √ 35 | GetTypeID 获取分类ID若分类不存在则返回0 √ 36 | SetHasPwd 开启并设置网站密码访问 √ 37 | CloseHasPwd 关闭网站密码访问 √ 38 | GetDirUserINI 获取网站几项开关(防跨站、日志、密码访问) √ 39 | WebAddSite 创建网站 √ 40 | WebDeleteSite 删除网站 √ 41 | WebSiteStop 停用网站 √ 42 | WebSiteStart 启用网站 √ 43 | WebSetEdate 设置网站有效期 √ 44 | WebSetPs 修改网站备注 √ 45 | WebBackupList 获取网站备份列表 √ 46 | WebToBackup 创建网站备份 √ 47 | WebDelBackup 删除网站备份 √ 48 | WebDoaminList 获取网站域名列表 √ 49 | GetDirBinding 获取网站域名绑定二级目录信息 √ 50 | AddDirBinding 添加网站子目录域名 √ 51 | DelDirBinding 删除网站绑定子目录 √ 52 | GetDirRewrite 获取网站子目录伪静态规则 √ 53 | WebAddDomain 添加网站域名 √ 54 | WebDelDomain 删除网站域名 √ 55 | GetSiteLogs 获取网站日志 √ 56 | GetSecurity 获取网站盗链状态及规则信息 √ 57 | SetSecurity 设置网站盗链状态及规则信息 √ 58 | GetSSL 获取SSL状态及证书详情 √ 59 | HttpToHttps 强制HTTPS √ 60 | CloseToHttps 关闭强制HTTPS √ 61 | SetSSL 设置SSL证书 √ 62 | CloseSSLConf 关闭SSL √ 63 | WebGetIndex 获取网站默认文件 √ 64 | WebSetIndex 设置网站默认文件 √ 65 | GetLimitNet 获取网站流量限制信息 √ 66 | SetLimitNet 设置网站流量限制信息 √ 67 | CloseLimitNet 关闭网站流量限制 √ 68 | Get301Status 获取网站301重定向信息 √ 69 | Set301Status 设置网站301重定向信息 √ 70 | GetRewriteList 获取可选的预定义伪静态列表 √ 71 | GetFileBody 获取指定预定义伪静态规则内容(获取文件内容) √ 72 | SaveFileBody 保存伪静态规则内容(保存文件内容) √ 73 | GetProxyList 获取网站反代信息及状态 √ 74 | CreateProxy 添加网站反代信息 √ 75 | ModifyProxy 修改网站反代信息 √ 76 | Ftp管理 ---803 77 | WebFtpList 获取FTP信息列表 √ 78 | SetUserPassword 修改FTP账号密码 √ 79 | GetFTPID 根据Ftp_Username获取FTPID √ 80 | SetStatus 启用/禁用FTP √ 81 | Sql管理 ---855 82 | WebSqlList 获取SQL信息列表 √ 83 | ResDatabasePass 修改SQL账号密码 √ 84 | GetSQLID 根据数据库名获取SQLID √ 85 | SQLToBackup 创建sql备份 √ 86 | SQLDelBackup 删除sql备份 √ 87 | download 下载备份文件 × 88 | 插件管理 ---913 89 | deployment 宝塔一键部署列表 √ 90 | SetupPackage 部署任务 √ 91 | """ 92 | config = { 93 | "AddDirBinding": "/site?action=AddDirBinding", 94 | "CloseHasPwd": "/site?action=CloseHasPwd", 95 | "CloseLimitNet": "/site?action=CloseLimitNet", 96 | "CloseSSLConf": "/site?action=CloseSSLConf", 97 | "CloseToHttps": "/site?action=CloseToHttps", 98 | "CreateProxy": "/site?action=CreateProxy", 99 | "DelDirBinding": "/site?action=DelDirBinding", 100 | "Get301Status": "/site?action=Get301Status", 101 | "GetDirBinding": "/site?action=GetDirBinding", 102 | "GetDirRewrite": "/site?action=GetDirRewrite", 103 | "GetDirUserINI": "/site?action=GetDirUserINI", 104 | "GetDiskInfo": "/system?action=GetDiskInfo", 105 | "GetFileBody": "/files?action=GetFileBody", 106 | "GetLimitNet": "/site?action=GetLimitNet", 107 | "GetNetWork": "/system?action=GetNetWork", 108 | "GetPHPVersion": "/site?action=GetPHPVersion", 109 | "GetProxyList": "/site?action=GetProxyList", 110 | "GetRewriteList": "/site?action=GetRewriteList", 111 | "GetSSL": "/site?action=GetSSL", 112 | "GetSecurity": "/site?action=GetSecurity", 113 | "GetSiteLogs": "/site?action=GetSiteLogs", 114 | "GetSitePHPVersion": "/site?action=GetSitePHPVersion", 115 | "GetSystemTotal": "/system?action=GetSystemTotal", 116 | "GetTaskCount": "/ajax?action=GetTaskCount", 117 | "HttpToHttps": "/site?action=HttpToHttps", 118 | "ModifyProxy": "/site?action=ModifyProxy", 119 | "ResDatabasePass": "/database?action=ResDatabasePassword", 120 | "SQLDelBackup": "/database?action=DelBackup", 121 | "SQLToBackup": "/database?action=ToBackup", 122 | "SaveFileBody": "/files?action=SaveFileBody", 123 | "Set301Status": "/site?action=Set301Status", 124 | "SetHasPwd": "/site?action=SetHasPwd", 125 | "SetLimitNet": "/site?action=SetLimitNet", 126 | "SetPHPVersion": "/site?action=SetPHPVersion", 127 | "SetSSL": "/site?action=SetSSL", 128 | "SetSecurity": "/site?action=SetSecurity", 129 | "SetStatus": "/ftp?action=SetStatus", 130 | "SetUserPassword": "/ftp?action=SetUserPassword", 131 | "SetupPackage": "/plugin?action=a&name=deployment&s=SetupPackage", 132 | "UpdatePanel": "/ajax?action=UpdatePanel", 133 | "WebAddDomain": "/site?action=AddDomain", 134 | "WebAddSite": "/site?action=AddSite", 135 | "WebBackupList": "/data?action=getData&table=backup", 136 | "WebDelBackup": "/site?action=DelBackup", 137 | "WebDelDomain": "/site?action=DelDomain", 138 | "WebDeleteSite": "/site?action=DeleteSite", 139 | "WebDoaminList": "/data?action=getData&table=domain", 140 | "WebFtpList": "/data?action=getData&table=ftps", 141 | "WebGetIndex": "/site?action=GetIndex", 142 | "WebSetEdate": "/site?action=SetEdate", 143 | "WebSetIndex": "/site?action=SetIndex", 144 | "WebSetPs": "/data?action=setPs&table=sites", 145 | "WebSiteStart": "/site?action=SiteStart", 146 | "WebSiteStop": "/site?action=SiteStop", 147 | "WebSqlList": "/data?action=getData&table=databases", 148 | "WebToBackup": "/site?action=ToBackup", 149 | "Websites": "/data?action=getData&table=sites", 150 | "Webtypes": "/site?action=get_site_types", 151 | "deployment": "/plugin?action=a&name=deployment&s=GetList&type=0", 152 | "download": "/download?filename=" 153 | } 154 | 155 | 156 | ## 导入依赖库 157 | import json 158 | import requests 159 | from time import time 160 | from hashlib import md5 161 | 162 | ## 定义主要函数 163 | def PingBaoTa(ip, port): # 拼接链接 164 | return "http://" + ip + ":" + port 165 | 166 | def GetMd5(s): # 参考了Demo.py 167 | m = md5() 168 | m.update(s.encode("utf-8")) 169 | return m.hexdigest() 170 | 171 | def GetKeyData(apikey): # 签名算法 172 | now_time = time() 173 | p_data = { 174 | "request_token": GetMd5(str(now_time) + "" + GetMd5(apikey)), 175 | "request_time": now_time 176 | } 177 | return p_data 178 | 179 | ## 定义功能(功能详见data.py) 180 | # 系统状态相关接口 181 | def GetSystemTotal(btpanel, btkey): # 获取系统基础统计 182 | url = btpanel + config["GetSystemTotal"] 183 | return requests.post(url, GetKeyData(btkey)).json() 184 | 185 | def GetDiskInfo(btpanel, btkey): # 获取磁盘分区信息 186 | url = btpanel + config["GetDiskInfo"] 187 | return requests.post(url, GetKeyData(btkey)).json() 188 | 189 | def GetNetWork(btpanel, btkey): # 获取实时状态信息(CPU、内存、网络、负载) 190 | url = btpanel + config["GetNetWork"] 191 | return requests.post(url, GetKeyData(btkey)).json() 192 | 193 | def GetTaskCount(btpanel, btkey): # 检查是否有安装任务 194 | url = btpanel + config["GetTaskCount"] 195 | return requests.post(url, GetKeyData(btkey)).json() 196 | 197 | def UpdatePanel(btpanel, btkey): # 检查面板更新 198 | url = btpanel + config["UpdatePanel"] 199 | return requests.post(url, GetKeyData(btkey)).json() 200 | 201 | # 网站管理相关接口 202 | def Websites(btpanel, btkey): # 获取网站列表 203 | url = btpanel + config["Websites"] 204 | return requests.post(url, GetKeyData(btkey)).json() 205 | 206 | def Webtypes(btpanel, btkey): # 获取网站分类 207 | url = btpanel + config["Webtypes"] 208 | return requests.post(url, GetKeyData(btkey)).json() 209 | 210 | def GetPHPVersion(btpanel, btkey): # 获取已安装的 PHP 版本列表 211 | url = btpanel + config["GetPHPVersion"] 212 | return requests.post(url, GetKeyData(btkey)).json() 213 | 214 | def GetSitePHPVersion(btpanel, btkey): # 获取指定网站运行的PHP版本 215 | url = btpanel + config["GetSitePHPVersion"] 216 | return requests.post(url, GetKeyData(btkey)).json() 217 | 218 | def SetPHPVersion(btpanel, btkey, site, php): # 修改指定网站PHP版本 $site=网站名;$php=PHP版本如73 219 | data = GetKeyData(btkey) 220 | data["siteName"] = site 221 | data["version"] = php 222 | url = btpanel + config["SetPHPVersion"] 223 | return requests.post(url, data).json() 224 | 225 | def GetSiteID(btpanel, btkey, site): # 获取指定站点ID 若站点不存在则返回-1 226 | data = Websites(btpanel, btkey)["data"] 227 | for i in data: 228 | if i["name"] == site: 229 | return i["id"] 230 | return -1 231 | 232 | def GetSitePath(btpanel, btkey, site): # 获取指定站点目录 若站点不存在则返回-1 233 | data = Websites(btpanel, btkey)["data"] 234 | for i in data: 235 | if i["name"] == site: 236 | return i["path"] 237 | return -1 238 | 239 | def SetHasPwd(btpanel, btkey, site, username, passwd): # 开启并设置网站密码访问 $site=网站名;$username=用户名;$passwd=密码 240 | data = GetKeyData(btkey) 241 | data["id"] = GetSiteID(btpanel, btkey, site) 242 | data["username"] = username 243 | data["password"] = passwd 244 | url = btpanel + config["SetHasPwd"] 245 | return requests.post(url, data).json() 246 | 247 | def CloseHasPwd(btpanel, btkey, site): # 关闭网站密码访问 $site=网站名 248 | data = GetKeyData(btkey) 249 | data["id"] = GetSiteID(btpanel, btkey, site) 250 | url = btpanel + config["CloseHasPwd"] 251 | return requests.post(url, data).json() 252 | 253 | def GetDirUserINI(btpanel, btkey, site): # 获取网站几项开关(防跨站、日志、密码访问)$site=网站名 254 | data = GetKeyData(btkey) 255 | data["id"] = GetSiteID(btpanel, btkey, site) 256 | data["path"] = GetSitePath(btpanel, btkey, site) 257 | url = btpanel + config["GetDirUserINI"] 258 | return requests.post(url, data).json() 259 | 260 | def GetTypeID(btpanel, btkey, _type): # 获取分类ID $_type=分类名,若不存在则返回0 261 | data = Webtypes(btpanel, btkey) 262 | for i in data: 263 | if i["name"] == _type: 264 | return i["id"] 265 | return 0 266 | 267 | def WebAddSite(btpanel, btkey, site, _type, ps, ftp="false", ftp_username=None,ftp_password=None, sql="false", sql_codeing="utf8mb4", datauser=None, datapassword=None): # 创建网站 268 | """ 269 | btpanel 面板地址,可用PingBaoTa()拼接 270 | btkey API密匙 271 | site 网站主域名 272 | _type 网站分类名 273 | ps 网站备注 274 | ftp="false" 是否开启FTP (true/false) 275 | ftp_username=None FTP用户名 276 | ftp_password=None FTP密码 277 | sql="false" 是否开启SQL (true/false) 278 | sql_codeing="utf8mb4" MySQL数据库格式,默认为utf8mb4 279 | datauser=None 数据库用户名 280 | datapassword=None 数据库密码 281 | """ 282 | data = GetKeyData(btkey) 283 | data["webname"] = json.dumps({"domain":site,"domainlist":[],"count":0}) 284 | data["path"] = "/www/wwwroot/" + site 285 | data["type_id"] = GetTypeID(btpanel, btkey, _type) 286 | data["type"] = "PHP" 287 | data["version"] = GetPHPVersion(btpanel, btkey)[-1]["version"] 288 | data["port"] = "80" 289 | data["ps"] = ps 290 | data["ftp"] = ftp 291 | if ftp_password != None and ftp_username != None and ftp == "true": 292 | data["ftp_username"] = ftp_username 293 | data["ftp_password"] = ftp_password 294 | data["sql"] = sql 295 | if datauser != None and datapassword != None and sql == "true": 296 | data["datauser"] = datauser 297 | data["datapassword"] = datapassword 298 | data["codeing"] = sql_codeing 299 | url = btpanel + config["WebAddSite"] 300 | return requests.post(url, data).json() 301 | 302 | def WebDeleteSite(btpanel, btkey, site, ftp="false", database="false", path="false"): # 删除网站 303 | """ 304 | btpanel 面板地址,可用PingBaoTa()拼接 305 | btkey API密匙 306 | site 网站名 307 | ftp="false" 是否删除FTP (true/false) 308 | database="false" 是否删除数据库 (true/false) 309 | path="false" 是否删除站点根目录 (true/false) 310 | """ 311 | url = btpanel + config["WebDeleteSite"] 312 | data = GetKeyData(btkey) 313 | data["id"] = GetSiteID(btpanel, btkey, site) 314 | data["webname"] = site 315 | data["ftp"] = ftp 316 | data["database"] = database 317 | data["path"] = path 318 | return requests.post(url, data).json() 319 | 320 | def WebSiteStop(btpanel, btkey, site): # 停用网站 321 | """ 322 | btpanel 面板地址,可用PingBaoTa()拼接 323 | btkey API密匙 324 | site 网站名 325 | """ 326 | url = btpanel + config["WebSiteStop"] 327 | data = GetKeyData(btkey) 328 | data["id"] = str(GetSiteID(btpanel, btkey, site)) 329 | data["name"] = site 330 | return requests.post(url, data).text 331 | 332 | def WebSiteStart(btpanel, btkey, site): # 启用网站 333 | """ 334 | btpanel 面板地址,可用PingBaoTa()拼接 335 | btkey API密匙 336 | site 网站名 337 | """ 338 | url = btpanel + config["WebSiteStart"] 339 | data = GetKeyData(btkey) 340 | data["id"] = GetSiteID(btpanel, btkey, site) 341 | data["name"] = site 342 | return requests.post(url, data).json() 343 | 344 | def WebSetEdate(btpanel, btkey, site, edate): # 设置网站到期时间 345 | """ 346 | btpanel 面板地址,可用PingBaoTa()拼接 347 | btkey API密匙 348 | site 网站名 349 | edate 到期时间 格式为xxxx-xx-xx 若需永久请输入0000-00-00 350 | """ 351 | url = btpanel + config["WebSetEdate"] 352 | data = GetKeyData(btkey) 353 | data["id"] = GetSiteID(btpanel, btkey, site) 354 | data["edate"] = edate 355 | return requests.post(url, data).json() 356 | 357 | def WebSetPs(btpanel, btkey, site, ps): # 修改网站备注 358 | """ 359 | btpanel 面板地址,可用PingBaoTa()拼接 360 | btkey API密匙 361 | site 网站名 362 | ps 备注 363 | """ 364 | url = btpanel + config["WebSetPs"] 365 | data = GetKeyData(btkey) 366 | data["id"] = GetSiteID(btpanel, btkey, site) 367 | data["ps"] = ps 368 | return requests.post(url, data).json() 369 | 370 | def WebBackupList(btpanel, btkey, site, p="1", limit="5", tojs="get_site_backup"): # 获取网站备份列表 371 | """ 372 | btpanel 面板地址,可用PingBaoTa()拼接 373 | btkey API密匙 374 | site 网站名 375 | p 当前分页 376 | limit 每页取回的数据行数 377 | tojs 分页 JS 回调,若不传则构造 URI 分页连接 378 | """ 379 | url = btpanel + config["WebBackupList"] 380 | data = GetKeyData(btkey) 381 | data["search"] = GetSiteID(btpanel, btkey, site) 382 | data["p"] = p 383 | data["limit"] = limit 384 | data["type"] = "0" 385 | data["tojs"] = tojs 386 | return requests.post(url, data).json() 387 | 388 | def WebToBackup(btpanel, btkey, site): # 创建网站备份 389 | """ 390 | btpanel 面板地址,可用PingBaoTa()拼接 391 | btkey API密匙 392 | site 网站名 393 | """ 394 | url = btpanel + config["WebToBackup"] 395 | data = GetKeyData(btkey) 396 | data["id"] = GetSiteID(btpanel, btkey, site) 397 | return requests.post(url, data).json() 398 | 399 | def WebDelBackup(btpanel, btkey, id): # 删除网站备份 400 | """ 401 | btpanel 面板地址,可用PingBaoTa()拼接 402 | btkey API密匙 403 | id 备份列表 ID 404 | """ 405 | url = btpanel + config["WebDelBackup"] 406 | data = GetKeyData(btkey) 407 | data["id"] = id 408 | return requests.post(url, data).json() 409 | 410 | def WebDoaminList(btpanel, btkey, site): # 获取网站域名列表 411 | """ 412 | btpanel 面板地址,可用PingBaoTa()拼接 413 | btkey API密匙 414 | site 网站名 415 | """ 416 | url = btpanel + config["WebDoaminList"] 417 | data = GetKeyData(btkey) 418 | data["search"] = GetSiteID(btpanel, btkey, site) 419 | data["list"] = "true" 420 | return requests.post(url, data).json() 421 | 422 | def GetDirBinding(btpanel, btkey, site): # 获取网站域名绑定二级目录信息 423 | """ 424 | btpanel 面板地址,可用PingBaoTa()拼接 425 | btkey API密匙 426 | site 网站名 427 | """ 428 | url = btpanel + config["GetDirBinding"] 429 | data = GetKeyData(btkey) 430 | data["id"] = GetSiteID(btpanel, btkey, site) 431 | return requests.post(url, data).json() 432 | 433 | def AddDirBinding(btpanel, btkey, site, domain, dirName): # 添加网站子目录域名 434 | """ 435 | btpanel 面板地址,可用PingBaoTa()拼接 436 | btkey API密匙 437 | site 网站名 438 | domain 域名 439 | dirName 目录 440 | """ 441 | url = btpanel + config["AddDirBinding"] 442 | data = GetKeyData(btkey) 443 | data["id"] = GetSiteID(btpanel, btkey, site) 444 | data["domain"] = domain 445 | data["dirName"] = dirName 446 | return requests.post(url, data).json() 447 | 448 | def DelDirBinding(btpanel, btkey, dirid): # 删除网站绑定子目录 449 | """ 450 | btpanel 面板地址,可用PingBaoTa()拼接 451 | btkey API密匙 452 | dirid 子目录ID 453 | """ 454 | url = btpanel + config["DelDirBinding"] 455 | data = GetKeyData(btkey) 456 | data["id"] = dirid 457 | return requests.post(url, data).json() 458 | 459 | def GetDirRewrite(btpanel, btkey, dirid): # 获取网站子目录绑定伪静态信息 460 | """ 461 | btpanel 面板地址,可用PingBaoTa()拼接 462 | btkey API密匙 463 | dirid 子目录ID 464 | """ 465 | url = btpanel + config["GetDirRewrite"] 466 | data = GetKeyData(btkey) 467 | data["id"] = dirid 468 | return requests.post(url, data).json() 469 | 470 | def WebAddDomain(btpanel, btkey, site, domain): # 添加网站域名 471 | """ 472 | btpanel 面板地址,可用PingBaoTa()拼接 473 | btkey API密匙 474 | site 网站名 475 | domain 新增的域名 476 | """ 477 | url = btpanel + config["WebAddDomain"] 478 | data = GetKeyData(btkey) 479 | data["id"] = GetSiteID(btpanel, btkey, site) 480 | data["webname"] = site 481 | data["domain"] = domain 482 | return requests.post(url, data).json() 483 | 484 | def WebDelDomain(btpanel, btkey, site, domain, port): # 删除网站域名 485 | """ 486 | btpanel 面板地址,可用PingBaoTa()拼接 487 | btkey API密匙 488 | site 网站名 489 | domain 删除的域名 490 | port 删除的域名的端口 491 | """ 492 | url = btpanel + config["WebDelDomain"] 493 | data = GetKeyData(btkey) 494 | data["id"] = GetSiteID(btpanel, btkey, site) 495 | data["webname"] = site 496 | data["domain"] = domain 497 | data["port"] = port 498 | return requests.post(url, data).json() 499 | 500 | def GetSiteLogs(btpanel, btkey, site): # 获取网站日志 501 | """ 502 | btpanel 面板地址,可用PingBaoTa()拼接 503 | btkey API密匙 504 | site 网站名 505 | """ 506 | url = btpanel + config["GetSiteLogs"] 507 | data = GetKeyData(btkey) 508 | data["siteName"] = site 509 | return requests.post(url, data).json() 510 | 511 | def GetSecurity(btpanel, btkey, site): # 获取网站盗链状态及规则信息 512 | """ 513 | btpanel 面板地址,可用PingBaoTa()拼接 514 | btkey API密匙 515 | site 域名 516 | """ 517 | url = btpanel + config["GetSecurity"] 518 | data = GetKeyData(btkey) 519 | data["id"] = GetSiteID(btpanel, btkey, site) 520 | data["name"] = site 521 | return requests.post(url, data).json() 522 | 523 | def SetSecurity(btpanel, btkey, site, fix, domains, status): # 获取网站盗链状态及规则信息 524 | """ 525 | btpanel 面板地址,可用PingBaoTa()拼接 526 | btkey API密匙 527 | site 网站名 528 | fix URL后缀 如"jpg,jpeg,gif,png,js,css" 529 | domains 许可域名 530 | status 启用防盗链状态: "true"/"false" 531 | """ 532 | url = btpanel + config["SetSecurity"] 533 | data = GetKeyData(btkey) 534 | data["id"] = GetSiteID(btpanel, btkey, site) 535 | data["name"] = site 536 | data["fix"] = fix 537 | data["domains"] = domains 538 | data["status"] = status 539 | return requests.post(url, data).json() 540 | 541 | def GetSSL(btpanel, btkey, site): # 获取SSL状态及证书详情 542 | """ 543 | btpanel 面板地址,可用PingBaoTa()拼接 544 | btkey API密匙 545 | site 域名 546 | """ 547 | url = btpanel + config["GetSSL"] 548 | data = GetKeyData(btkey) 549 | data["siteName"] = site 550 | return requests.post(url, data).json() 551 | 552 | def HttpToHttps(btpanel, btkey, site): # 开启强制HTTPS 553 | """ 554 | btpanel 面板地址,可用PingBaoTa()拼接 555 | btkey API密匙 556 | site 域名 557 | """ 558 | url = btpanel + config["HttpToHttps"] 559 | data = GetKeyData(btkey) 560 | data["siteName"] = site 561 | return requests.post(url, data).json() 562 | 563 | def CloseToHttps(btpanel, btkey, site): # 关闭强制HTTPS 564 | """ 565 | btpanel 面板地址,可用PingBaoTa()拼接 566 | btkey API密匙 567 | site 域名 568 | """ 569 | url = btpanel + config["CloseToHttps"] 570 | data = GetKeyData(btkey) 571 | data["siteName"] = site 572 | return requests.post(url, data).json() 573 | 574 | def SetSSL(btpanel, btkey, site, key, csr, type="1"): # 设置SSL证书 575 | """ 576 | btpanel 面板地址,可用PingBaoTa()拼接 577 | btkey API密匙 578 | type 类型 (暂不明确用途 579 | site 域名 580 | key 证书key 581 | csr 证书PEM 582 | """ 583 | url = btpanel + config["SetSSL"] 584 | data = GetKeyData(btkey) 585 | data["type"] = type 586 | data["siteName"] = site 587 | data["key"] = key 588 | data["csr"] = csr 589 | return requests.post(url, data).json() 590 | 591 | def CloseSSLConf(btpanel, btkey, site, updateOf="1"): # 关闭SSL 592 | """ 593 | btpanel 面板地址,可用PingBaoTa()拼接 594 | btkey API密匙 595 | site 域名 596 | updateOf 修改状态码 (暂不明确用途 597 | """ 598 | url = btpanel + config["CloseSSLConf"] 599 | data = GetKeyData(btkey) 600 | data["siteName"] = site 601 | data["updateOf"] = updateOf 602 | return requests.post(url, data).json() 603 | 604 | def WebGetIndex(btpanel, btkey, site): # 获取网站默认文件 605 | """ 606 | btpanel 面板地址,可用PingBaoTa()拼接 607 | btkey API密匙 608 | site 网站名 609 | """ 610 | url = btpanel + config["WebGetIndex"] 611 | data = GetKeyData(btkey) 612 | data["id"] = GetSiteID(btpanel, btkey, site) 613 | return requests.post(url, data).json() 614 | 615 | def WebSetIndex(btpanel, btkey, site, index): # 设置网站默认文件 616 | """ 617 | btpanel 面板地址,可用PingBaoTa()拼接 618 | btkey API密匙 619 | site 网站名 620 | index 默认文件内容,如 "api.php,index.php,index.html,index.htm,default.php,default.htm,default.html" 621 | """ 622 | url = btpanel + config["WebSetIndex"] 623 | data = GetKeyData(btkey) 624 | data["id"] = GetSiteID(btpanel, btkey, site) 625 | data["Index"] = index 626 | return requests.post(url, data).json() 627 | 628 | def GetLimitNet(btpanel, btkey, site): # 获取网站流量限制信息 629 | """ 630 | btpanel 面板地址,可用PingBaoTa()拼接 631 | btkey API密匙 632 | site 网站名 633 | """ 634 | url = btpanel + config["GetLimitNet"] 635 | data = GetKeyData(btkey) 636 | data["id"] = GetSiteID(btpanel, btkey, site) 637 | return requests.post(url, data).json() 638 | 639 | def SetLimitNet(btpanel, btkey, site, perserver, perip, limit_rate): # 设置网站流量限制信息 640 | """ 641 | btpanel 面板地址,可用PingBaoTa()拼接 642 | btkey API密匙 643 | site 网站名 644 | perserver 并发限制 645 | perip 单IP限制 646 | limit_rate 流量限制 647 | """ 648 | url = btpanel + config["GetLimitNet"] 649 | data = GetKeyData(btkey) 650 | data["id"] = GetSiteID(btpanel, btkey, site) 651 | data["perserver"] = perserver 652 | data["perip"] = perip 653 | data["limit_rate"] = limit_rate 654 | return requests.post(url, data).json() 655 | 656 | def CloseLimitNet(btpanel, btkey, site): # 关闭网站流量限制 657 | """ 658 | btpanel 面板地址,可用PingBaoTa()拼接 659 | btkey API密匙 660 | site 网站名 661 | """ 662 | url = btpanel + config["CloseLimitNet"] 663 | data = GetKeyData(btkey) 664 | data["id"] = GetSiteID(btpanel, btkey, site) 665 | return requests.post(url, data).json() 666 | 667 | def Get301Status(btpanel, btkey, site): # 获取网站301重定向信息 668 | """ 669 | btpanel 面板地址,可用PingBaoTa()拼接 670 | btkey API密匙 671 | site 网站名 672 | """ 673 | url = btpanel + config["Get301Status"] 674 | data = GetKeyData(btkey) 675 | data["siteName"] = site 676 | return requests.post(url, data).json() 677 | 678 | def Set301Status(btpanel, btkey, site, toDomain, srcDomain, type): # 设置网站301重定向信息 679 | """ 680 | btpanel 面板地址,可用PingBaoTa()拼接 681 | btkey API密匙 682 | site 网站名 683 | toDomain 目标Url 684 | srcDomain 来自Url 685 | type 类型 686 | """ 687 | url = btpanel + config["Set301Status"] 688 | data = GetKeyData(btkey) 689 | data["siteName"] = site 690 | data['toDomain'] = toDomain 691 | data['srcDomain'] = srcDomain 692 | data['type'] = type 693 | return requests.post(url, data).json() 694 | 695 | def GetRewriteList(btpanel, btkey, site): # 获取可选的预定义伪静态列表 696 | """ 697 | btpanel 面板地址,可用PingBaoTa()拼接 698 | btkey API密匙 699 | site 网站名 700 | """ 701 | url = btpanel + config["GetRewriteList"] 702 | data = GetKeyData(btkey) 703 | data["siteName"] = site 704 | return requests.post(url, data).json() 705 | 706 | def GetFileBody(btpanel, btkey, path, type): # 获取预置伪静态规则内容(文件内容) 707 | """ 708 | btpanel 面板地址,可用PingBaoTa()拼接 709 | btkey API密匙 710 | path 规则名(站点名) 711 | type 0->获取内置伪静态规则;1->获取当前站点伪静态规则 712 | """ 713 | url = btpanel + config["GetFileBody"] 714 | data = GetKeyData(btkey) 715 | if type: 716 | path_dir = "vhost/rewrite" 717 | else: 718 | path_dir = "rewrite/nginx" 719 | """ 720 | 获取当前站点伪静态规则 721 | /www/server/panel/vhost/rewrite/user_hvVBT_1.test.com.conf 722 | 获取内置伪静态规则 723 | /www/server/panel/rewrite/nginx/EmpireCMS.conf 724 | 保存伪静态规则到站点 725 | /www/server/panel/vhost/rewrite/user_hvVBT_1.test.com.conf 726 | /www/server/panel/rewrite/nginx/typecho.conf 727 | """ 728 | data['path'] = '/www/server/panel/' + path_dir + '/' + path + '.conf' 729 | return requests.post(url, data).json() 730 | 731 | def SaveFileBody(btpanel, btkey, path, _data, encoding="utf-8", type=0): # 保存伪静态规则内容(保存文件内容) 732 | """ 733 | btpanel 面板地址,可用PingBaoTa()拼接 734 | btkey API密匙 735 | path 规则名(站点名) 736 | _data 规则内容 737 | encoding 规则编码强转utf-8 738 | type 0->系统默认路径;1->自定义全路径 739 | """ 740 | url = btpanel + config["SaveFileBody"] 741 | data = GetKeyData(btkey) 742 | if type: 743 | path_dir = path 744 | else: 745 | path_dir = '/www/server/panel/vhost/rewrite/' + path + '.conf' 746 | data['path'] = path_dir 747 | data['data'] = _data 748 | data['encoding'] = encoding 749 | return requests.post(url, data).json() 750 | 751 | def GetProxyList(btpanel, btkey, site): # 获取网站反代信息及状态 752 | """ 753 | btpanel 面板地址,可用PingBaoTa()拼接 754 | btkey API密匙 755 | site 网站名 756 | """ 757 | url = btpanel + config["GetProxyList"] 758 | data = GetKeyData(btkey) 759 | data["sitename"] = site 760 | return requests.post(url, data).json() 761 | 762 | def ModifyProxy(btpanel, btkey, cache, proxyname, cachetime, proxydir, proxysite, todomain, advanced, sitename, subfilter, type): # 修改网站反代信息 763 | """ 764 | btpanel 面板地址,可用PingBaoTa()拼接 765 | btkey API密匙 766 | cache 是否缓存 767 | proxyname 代理名称 768 | cachetime 缓存时长 /小时 769 | proxydir 代理目录 770 | proxysite 反代URL 771 | todomain 目标域名 772 | advanced 高级功能:开启代理目录 773 | sitename 网站名 774 | subfilter 文本替换json格式[{"sub1":"百度","sub2":"白底"},{"sub1":"","sub2":""}] 775 | type 开启或关闭 0关;1开 776 | """ 777 | url = btpanel + config["GetProxyList"] 778 | data = GetKeyData(btkey) 779 | data['cache'] = cache 780 | data['proxyname'] = proxyname 781 | data['cachetime'] = cachetime 782 | data['proxydir'] = proxydir 783 | data['proxysite'] = proxysite 784 | data['todomain'] = todomain 785 | data['advanced'] = advanced 786 | data['sitename'] = sitename 787 | data['subfilter'] = subfilter 788 | data['type'] = type 789 | return requests.post(url, data).json() 790 | 791 | def CreateProxy(btpanel, btkey, cache, proxyname, cachetime, proxydir, proxysite, todomain, advanced, sitename, subfilter, type): # 添加网站反代信息 792 | """ 793 | btpanel 面板地址,可用PingBaoTa()拼接 794 | btkey API密匙 795 | cache 是否缓存 796 | proxyname 代理名称 797 | cachetime 缓存时长 /小时 798 | proxydir 代理目录 799 | proxysite 反代URL 800 | todomain 目标域名 801 | advanced 高级功能:开启代理目录 802 | sitename 网站名 803 | subfilter 文本替换json格式[{"sub1":"百度","sub2":"白底"},{"sub1":"","sub2":""}] 804 | type 开启或关闭 0关;1开 805 | """ 806 | url = btpanel + config["CreateProxy"] 807 | data = GetKeyData(btkey) 808 | data['cache'] = cache 809 | data['proxyname'] = proxyname 810 | data['cachetime'] = cachetime 811 | data['proxydir'] = proxydir 812 | data['proxysite'] = proxysite 813 | data['todomain'] = todomain 814 | data['advanced'] = advanced 815 | data['sitename'] = sitename 816 | data['subfilter'] = subfilter 817 | data['type'] = type 818 | return requests.post(url, data).json() 819 | 820 | # Ftp管理 821 | def WebFtpList(btpanel, btkey, page='1', limit='15', type='-1', order='id desc', tojs='', search=''): # 获取网站FTP列表 822 | """ 823 | page 当前分页 824 | limit 取出的数据行数 825 | type 分类标识 -1: 分部分类 0: 默认分类 826 | order 排序规则 使用 id 降序:id desc 使用名称升序:name desc 827 | tojs 分页 JS 回调,若不传则构造 URI 分页连接 828 | search 搜索内容 829 | """ 830 | url = btpanel + config["WebFtpList"] 831 | data = GetKeyData(btkey) 832 | data['p'] = page 833 | data['limit'] = limit 834 | data['type'] = type 835 | data['order'] = order 836 | data['tojs'] = tojs 837 | data['search'] = search 838 | return requests.post(url, data).json() 839 | 840 | def GetFTPID(btpanel, btkey, ftp_username): # 根据Ftp_Username获取FTPID 841 | # ftp_username为FTP用户名 842 | data = WebFtpList(btpanel, btkey) 843 | for i in data['data']: 844 | if i['name'] == ftp_username: 845 | return i['id'] 846 | return -1 847 | 848 | def SetUserPassword(btpanel, btkey, ftp_username, new_password): # 修改FTP账号密码 849 | """ 850 | ftp_username 用户名 851 | new_password 密码 852 | """ 853 | url = btpanel + config["SetUserPassword"] 854 | data = GetKeyData(btkey) 855 | data["id"] = GetFTPID(btpanel, btkey, ftp_username) 856 | data["ftp_username"] = ftp_username 857 | data["new_password"] = new_password 858 | return requests.post(url, data).json() 859 | 860 | def SetStatus(btpanel, btkey, ftp_username, status): # 启用/禁用FTP 861 | """ 862 | ftp_username 用户名 863 | status 状态 0->关闭;1->开启 864 | """ 865 | url = btpanel + config["SetStatus"] 866 | data = GetKeyData(btkey) 867 | data["id"] = GetFTPID(btpanel, btkey, ftp_username) 868 | data["username"] = ftp_username 869 | data["status"] = status 870 | return requests.post(url, data).json() 871 | 872 | # Sql管理 873 | def WebSqlList(btpanel, btkey, page='1', limit='15', type='-1', order='id desc', tojs='', search=''): # 获取SQL信息列表 874 | """ 875 | page 当前分页 876 | limit 取出的数据行数 877 | type 分类标识 -1: 分部分类 0: 默认分类 878 | order 排序规则 使用 id 降序:id desc 使用名称升序:name desc 879 | tojs 分页 JS 回调,若不传则构造 URI 分页连接 880 | search 搜索内容 881 | """ 882 | url = btpanel + config["WebSqlList"] 883 | data = GetKeyData(btkey) 884 | data['p'] = page 885 | data['limit'] = limit 886 | data['type'] = type 887 | data['order'] = order 888 | data['tojs'] = tojs 889 | data['search'] = search 890 | return requests.post(url, data).json() 891 | 892 | def GetSQLID(btpanel, btkey, sql_username): # 根据数据库名获取SQLID 893 | # sql_username为SQL用户名 894 | data = WebSqlList(btpanel, btkey) 895 | for i in data['data']: 896 | if i['name'] == sql_username: 897 | return i['id'] 898 | return -1 899 | 900 | def ResDatabasePass(btpanel, btkey, ftp_username, new_password): # 修改数据库账号密码 901 | """ 902 | ftp_username 数据库名 903 | new_password 新密码 904 | """ 905 | url = btpanel + config["ResDatabasePass"] 906 | data = GetKeyData(btkey) 907 | data["id"] = GetSQLID(btpanel, btkey, ftp_username) 908 | data["name"] = ftp_username 909 | data["password"] = new_password 910 | return requests.post(url, data).json() 911 | 912 | def SQLToBackup(btpanel, btkey, database_name): # 创建sql备份 913 | """ 914 | database_name 数据库名 915 | """ 916 | url = btpanel + config["SQLToBackup"] 917 | data = GetKeyData(btkey) 918 | data["id"] = GetSQLID(btpanel, btkey, database_name) 919 | return requests.post(url, data).json() 920 | 921 | def SQLDelBackup(btpanel, btkey, id): # 删除sql备份 922 | """ 923 | id 备份ID 924 | """ 925 | url = btpanel + config["SQLDelBackup"] 926 | data = GetKeyData(btkey) 927 | data["id"] = id 928 | return requests.post(url, data).json() 929 | 930 | # 插件管理 931 | def deployment(btpanel, btkey, search=False): # 获取宝塔一键部署列表 932 | """ 933 | search 搜索关键词 934 | """ 935 | url = btpanel + config["deployment"] 936 | if search: 937 | url = url + "&search=" + search 938 | print(url) 939 | data = GetKeyData(btkey) 940 | return requests.post(url, data).json() 941 | 942 | def SetupPackage(btpanel, btkey, dname, site_name, php_version): # 宝塔一键部署执行 943 | """ 944 | dname 部署程序名 945 | site_name 部署到网站名 946 | php_version PHP版本 947 | """ 948 | url = btpanel + config["SetupPackage"] 949 | data = GetKeyData(btkey) 950 | data['dname'] = dname 951 | data['site_name'] = site_name 952 | data['php_version'] = php_version 953 | return requests.post(url, data).json() 954 | 955 | # Demo 956 | if __name__ == "__main__": 957 | _ApiKey = "xxxxxxxxxxxxxxxx" 958 | _BtPanel = "127.0.0.1" 959 | _Port = "8888" 960 | _Url = PingBaoTa(_BtPanel, _Port) 961 | print(GetSystemTotal(_Url, _ApiKey)) 962 | --------------------------------------------------------------------------------