diff --git a/utils/http.py b/utils/http.py index dd97daa6..90c45885 100644 --- a/utils/http.py +++ b/utils/http.py @@ -14,18 +14,12 @@ def post(url, body, logger, headers=None, timeout=5) : } result = {} - try : - # 消除https警告 - requests.packages.urllib3.disable_warnings() - except Exception : - pass - response = None try : if headers : - response = requests.post(url, headers=headers, data=json.dumps(body), proxies=proxies, verify=False, timeout=timeout) + response = requests.post(url, headers=headers, data=json.dumps(body), proxies=proxies, verify=True, timeout=timeout) else : - response = requests.post(url, data=json.dumps(body), proxies=proxies, verify=False, timeout=timeout) + response = requests.post(url, data=json.dumps(body), proxies=proxies, verify=True, timeout=timeout) try : response.encoding = "utf-8" result = json.loads(response.text) @@ -100,7 +94,7 @@ def getOCR(url) : proxies = {"http": None, "https": None} start = time.time() try : - res = requests.get(url, proxies=proxies, verify=False, timeout=3) + res = requests.get(url, proxies=proxies, verify=True, timeout=3) time_diff = time.time() - start status_code = res.status_code except Exception : diff --git a/utils/offline_ocr.py b/utils/offline_ocr.py index 3d608457..6138434d 100644 --- a/utils/offline_ocr.py +++ b/utils/offline_ocr.py @@ -5,6 +5,7 @@ import time import requests import zipfile +import subprocess import utils.message import utils.thread @@ -47,10 +48,11 @@ def install_offline_ocr(object) : def killOfflineOCR(port) : try : - popen_content = os.popen("netstat -ano |findstr %s"%port).read() - if popen_content : - pid = popen_content.split(" ")[-1] - os.popen("taskkill /f /t /im %s"%pid) + popen_content = subprocess.run(["netstat", "-ano"], capture_output=True, text=True).stdout + lines = [line for line in popen_content.splitlines() if str(port) in line] + if lines: + pid = lines[0].split()[-1] + subprocess.run(["taskkill", "/f", "/t", "/pid", pid], capture_output=True) except Exception : return format_exc() diff --git a/utils/sqlite.py b/utils/sqlite.py index 267a4aba..19a4f5a0 100644 --- a/utils/sqlite.py +++ b/utils/sqlite.py @@ -130,15 +130,18 @@ def selectTranslationDBList(src, tgt, limit, offset, logger) : try : if src : if tgt : - sql = '''SELECT id, trans_type, src, tgt FROM translations WHERE src LIKE '%{}%' AND tgt LIKE '%{}%' ORDER BY id DESC LIMIT ? OFFSET ?;'''.format(src, tgt) + sql = '''SELECT id, trans_type, src, tgt FROM translations WHERE src LIKE ? AND tgt LIKE ? ORDER BY id DESC LIMIT ? OFFSET ?;''' + cursor = TRANSLATION_DB.execute(sql, (f'%{src}%', f'%{tgt}%', limit, offset)) else : - sql = '''SELECT id, trans_type, src, tgt FROM translations WHERE src LIKE '%{}%' ORDER BY id DESC LIMIT ? OFFSET ?;'''.format(src) + sql = '''SELECT id, trans_type, src, tgt FROM translations WHERE src LIKE ? ORDER BY id DESC LIMIT ? OFFSET ?;''' + cursor = TRANSLATION_DB.execute(sql, (f'%{src}%', limit, offset)) else: if tgt : - sql = '''SELECT id, trans_type, src, tgt FROM translations WHERE tgt LIKE '%{}%' ORDER BY id DESC LIMIT ? OFFSET ?;'''.format(tgt) + sql = '''SELECT id, trans_type, src, tgt FROM translations WHERE tgt LIKE ? ORDER BY id DESC LIMIT ? OFFSET ?;''' + cursor = TRANSLATION_DB.execute(sql, (f'%{tgt}%', limit, offset)) else : sql = '''SELECT id, trans_type, src, tgt FROM translations ORDER BY id DESC LIMIT ? OFFSET ?;''' - cursor = TRANSLATION_DB.execute(sql, (limit, offset)) + cursor = TRANSLATION_DB.execute(sql, (limit, offset)) rows = cursor.fetchall() cursor.close() except Exception : @@ -233,15 +236,18 @@ def selectTranslationDBTotal(src, tgt, logger) : try : if src : if tgt : - sql = '''SELECT count(*) FROM translations WHERE src LIKE '%{}%' AND tgt LIKE '%{}%';'''.format(src, tgt) + sql = '''SELECT count(*) FROM translations WHERE src LIKE ? AND tgt LIKE ?;''' + cursor = TRANSLATION_DB.execute(sql, (f'%{src}%', f'%{tgt}%')) else : - sql = '''SELECT count(*) FROM translations WHERE src LIKE '%{}%';'''.format(src) + sql = '''SELECT count(*) FROM translations WHERE src LIKE ?;''' + cursor = TRANSLATION_DB.execute(sql, (f'%{src}%',)) else : if tgt : - sql = '''SELECT count(*) FROM translations WHERE tgt LIKE '%{}%';'''.format(tgt) + sql = '''SELECT count(*) FROM translations WHERE tgt LIKE ?;''' + cursor = TRANSLATION_DB.execute(sql, (f'%{tgt}%',)) else : sql = '''SELECT count(*) FROM translations;''' - cursor = TRANSLATION_DB.execute(sql) + cursor = TRANSLATION_DB.execute(sql) result = cursor.fetchone()[0] cursor.close() except Exception :