diff --git a/plugins/openresty/index.html b/plugins/openresty/index.html index 30803d9f1..086bec6de 100755 --- a/plugins/openresty/index.html +++ b/plugins/openresty/index.html @@ -1,5 +1,5 @@
服务
diff --git a/plugins/openresty/index.py b/plugins/openresty/index.py index 217634a6f..778324890 100755 --- a/plugins/openresty/index.py +++ b/plugins/openresty/index.py @@ -8,8 +8,12 @@ import threading import subprocess import re -sys.path.append(os.getcwd() + "/class/core") -import mw + +web_dir = os.getcwd() + "/web" +sys.path.append(web_dir) +os.chdir(web_dir) + +import core.mw as mw app_debug = False if mw.isAppleSystem(): @@ -41,18 +45,19 @@ def getInitDFile(): def getArgs(): args = sys.argv[2:] + # print(args) tmp = {} args_len = len(args) if args_len == 1: t = args[0].strip('{').strip('}') - t = t.split(':') + t = t.split(':',2) tmp[t[0]] = t[1] elif args_len > 1: for i in range(len(args)): - t = args[i].split(':') + t = args[i].split(':',2) tmp[t[0]] = t[1] - + # print(tmp) return tmp @@ -398,16 +403,25 @@ def initdUinstall(): mw.execShell('systemctl disable openresty') return 'ok' +def getNgxStatusPort(): + ngx_status_file = mw.getServerDir() + '/web_conf/nginx/vhost/0.nginx_status.conf' + content = mw.readFile(ngx_status_file) + rep = r'listen\s*(.*);' + tmp = re.search(rep, content) + port = tmp.groups()[0].strip() + return port + def runInfo(): op_status = status() if op_status == 'stop': return mw.returnJson(False, "未启动!") + port = getNgxStatusPort() # 取Openresty负载状态 try: - url = 'http://127.0.0.1/nginx_status' - result = mw.httpGet(url, timeout=1) + url = 'http://127.0.0.1:%s/nginx_status' % port + result = mw.httpGet(url, timeout=3) tmp = result.split() data = {} data['active'] = tmp[2] @@ -419,8 +433,7 @@ def runInfo(): data['Waiting'] = tmp[15] return mw.getJson(data) except Exception as e: - - url = 'http://' + mw.getHostAddr() + '/nginx_status' + url = 'http://' + mw.getHostAddr() + ':%s/nginx_status' % port result = mw.httpGet(url) tmp = result.split() data = {} @@ -550,6 +563,13 @@ def installPreInspection(): if __name__ == "__main__": + version_pl = getServerDir() + "/version.pl" + if not os.path.exists(version_pl): + print('error') + exit(-1) + version = mw.readFile(version_pl) + + func = sys.argv[1] if func == 'status': print(status()) diff --git a/web/core/mw.py b/web/core/mw.py index d8773b439..8ec077999 100644 --- a/web/core/mw.py +++ b/web/core/mw.py @@ -199,7 +199,7 @@ def readFile(filename): fp.close() return fBody except Exception as e: - # print(e) + print(e) return False def writeFile(filename, content, mode='w+'): @@ -764,6 +764,112 @@ def deDoubleCrypt(key, strings): writeFileLog(getTracebackInfo()) return strings +# ------------------------------ network start ----------------------------- + +def HttpGet(url, timeout=10): + """ + 发送GET请求 + @url 被请求的URL地址(必需) + @timeout 超时时间默认60秒 + return string + """ + if sys.version_info[0] == 2: + try: + import urllib2 + import ssl + if sys.version_info[0] == 2: + reload(urllib2) + reload(ssl) + try: + ssl._create_default_https_context = ssl._create_unverified_context + except: + pass + response = urllib2.urlopen(url, timeout=timeout) + return response.read() + except Exception as ex: + return str(ex) + else: + try: + import urllib.request + import ssl + try: + ssl._create_default_https_context = ssl._create_unverified_context + except: + pass + response = urllib.request.urlopen(url, timeout=timeout) + result = response.read() + if type(result) == bytes: + result = result.decode('utf-8') + return result + except Exception as ex: + return str(ex) + + +def HttpGet2(url, timeout): + import urllib.request + + try: + import ssl + try: + ssl._create_default_https_context = ssl._create_unverified_context + except: + pass + req = urllib.request.urlopen(url, timeout=timeout) + result = req.read().decode('utf-8') + return result + + except Exception as e: + return str(e) + + +def httpGet(url, timeout=10): + return HttpGet2(url, timeout) + + +def HttpPost(url, data, timeout=10): + """ + 发送POST请求 + @url 被请求的URL地址(必需) + @data POST参数,可以是字符串或字典(必需) + @timeout 超时时间默认60秒 + return string + """ + if sys.version_info[0] == 2: + try: + import urllib + import urllib2 + import ssl + ssl._create_default_https_context = ssl._create_unverified_context + data = urllib.urlencode(data) + req = urllib2.Request(url, data) + response = urllib2.urlopen(req, timeout=timeout) + return response.read() + except Exception as ex: + return str(ex) + else: + try: + import urllib.request + import ssl + try: + ssl._create_default_https_context = ssl._create_unverified_context + except: + pass + data = urllib.parse.urlencode(data).encode('utf-8') + req = urllib.request.Request(url, data) + response = urllib.request.urlopen(req, timeout=timeout) + result = response.read() + if type(result) == bytes: + result = result.decode('utf-8') + return result + except Exception as ex: + return str(ex) + + +def httpPost(url, data, timeout=10): + return HttpPost(url, data, timeout) + +# ------------------------------ network end ----------------------------- + # ------------------------------ panel start ----------------------------- def isRestart():