有个问题,请教下各位大神,我在用python去添加网站的时候总是报500错误,其他像获取系统信息,获取LOG日志等都正常的。python文件如下,python本版:3.7.0
- #coding: utf-8
- # +-------------------------------------------------------------------
- # | 宝塔Linux面板
- # +-------------------------------------------------------------------
- # | Copyright (c) 2015-2099 宝塔软件() All rights reserved.
- # +-------------------------------------------------------------------
- # | Author: 黄文良 <2879625666@qq.com>
- # +-------------------------------------------------------------------
- #------------------------------
- # API-Demo of Python
- #------------------------------
- import time,hashlib,sys,os,json
- class bt_api:
- __BT_KEY = '***'
- __BT_PANEL = '***'
- #如果希望多台面板,可以在实例化对象时,将面板地址与密钥传入
- 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)
- #取系统状态
- def sysstatus(self):
- #拼接URL地址
- url = self.__BT_PANEL + '/system?action=GetSystemTotal'
- #准备POST数据
- p_data = self.__get_key_data() #取签名
- #请求面板接口
- result = self.__http_post_cookie(url,p_data)
- #解析JSON数据
- return json.loads(result)
- #获取磁盘信息
- def diskinfo(self):
- #拼接URL地址
- url = self.__BT_PANEL + '/system?action=GetDiskInfo'
- #准备POST数据
- p_data = self.__get_key_data() #取签名
- #请求面板接口
- result = self.__http_post_cookie(url,p_data)
- #解析JSON数据
- return json.loads(result)
- def addsite(self):
- #拼接URL地址
- url = self.__BT_PANEL + '/site?action=AddSite'
- #准备POST数据
- p_data = self.__get_key_data() #取签名
- # p_data['tojs'] = 'test'
- # p_data={}
- p_data['webname'] = {"domain":'test.com',"domainlist":[],"count":0}
- p_data['path'] = '/www/wwwroot/test.com'
- p_data['type_id'] = 0
- p_data['type'] = 'PHP'
- p_data['version'] = 72
- p_data['port'] = 80
- p_data['ps'] = 'test'
- p_data['ftp'] = False
- p_data['ftp_username'] = 'testtest'
- p_data['ftp_password'] = 'testtest'
- p_data['sql'] = False
- p_data['coding'] = 'utf8'
- p_data['datauser'] = 'testtest'
- p_data['datapassword'] = 'testtesttesttest'
- #请求面板接口
- 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=18000):
- cookie_file = './' + self.__get_md5(self.__BT_PANEL) + '.cookie'
- with open(cookie_file,"a") as file:
- file.write("# Netscape HTTP Cookie File\n")
- 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
- cookie_obj = http.cookiejar.MozillaCookieJar(cookie_file)
- if os.path.exists(cookie_file):
- cookie_obj.load(cookie_file,ignore_discard=True,ignore_expires=True)
- # 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')
- print(data)
- 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对象
- my_api = bt_api()
- domain = 'test.laojihost.com'
- username = str(domain)[0-15]
- # 调用get_logs方法
- r_data = my_api.get_logs()
- # r_sys = my_api.sysstatus()
- # r_disk = my_api.diskinfo()
- r_add_site = my_api.addsite()
- #打印响应数据
- print(r_data)
- # print(r_sys)
- # print(r_disk)
- print(r_add_site)
复制代码
运行报错如下:
- Traceback (most recent call last):
- File "demo.py", line 169, in <module>
- r_add_site = my_api.addsite()
- File "demo.py", line 91, in addsite
- result = self.__http_post_cookie(url,p_data)
- File "demo.py", line 136, in __http_post_cookie
- response = opener.open(req,timeout=timeout)
- File "/usr/lib64/python2.7/urllib2.py", line 437, in open
- response = meth(req, response)
- File "/usr/lib64/python2.7/urllib2.py", line 550, in http_response
- 'http', request, response, code, msg, hdrs)
- File "/usr/lib64/python2.7/urllib2.py", line 475, in error
- return self._call_chain(*args)
- File "/usr/lib64/python2.7/urllib2.py", line 409, in _call_chain
- result = func(*args)
- File "/usr/lib64/python2.7/urllib2.py", line 558, in http_error_default
- raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
- urllib2.HTTPError: HTTP Error 500: INTERNAL SERVER ERROR
复制代码 |