From 2d7c413ed93f5ddc9eebfac7274a4d793d71a3f1 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Thu, 12 Oct 2023 15:46:05 +0800 Subject: [PATCH] bugfix --- ahserver/baseProcessor.py | 13 +++++++------ ahserver/dsProcessor.py | 5 +++-- ahserver/processorResource.py | 15 +++++++-------- ahserver/websocketProcessor.py | 10 ++++++---- requirements.txt | 1 + 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/ahserver/baseProcessor.py b/ahserver/baseProcessor.py index 4e778fe..13c968b 100755 --- a/ahserver/baseProcessor.py +++ b/ahserver/baseProcessor.py @@ -2,6 +2,7 @@ import os import re import json import codecs +import aiofiles from aiohttp.web_request import Request from aiohttp.web_response import Response, StreamResponse @@ -168,10 +169,10 @@ class PythonScriptProcessor(BaseProcessor): def isMe(self,name): return name=='dspy' - def loadScript(self, path): + async def loadScript(self, path): data = '' - with codecs.open(path,'rb','utf-8') as f: - data = f.read() + async with aiofiles.open(path,'r', encoding='utf-8') as f: + data = await f.read() b= ''.join(data.split('\r')) lines = b.split('\n') lines = ['\t' + l for l in lines ] @@ -182,7 +183,7 @@ class PythonScriptProcessor(BaseProcessor): await self.set_run_env(request, params=params) lenv = self.run_ns del lenv['request'] - txt = self.loadScript(self.real_path) + txt = await self.loadScript(self.real_path) # print(self.real_path, "#########", txt) exec(txt,lenv,lenv) func = lenv['myfunc'] @@ -198,8 +199,8 @@ class MarkdownProcessor(BaseProcessor): async def datahandle(self,request:Request): data = '' - with codecs.open(self.real_path,'rb','utf-8') as f: - data = f.read() + async with aiofiles.open(self.real_path,'r',encoding='utf-8') as f: + data = await f.read() self.content = self.urlreplace(data, request) def urlreplace(self,mdtxt,request): diff --git a/ahserver/dsProcessor.py b/ahserver/dsProcessor.py index 054833c..02e97da 100755 --- a/ahserver/dsProcessor.py +++ b/ahserver/dsProcessor.py @@ -1,5 +1,6 @@ import codecs import json +import aiofiles from appPublic.jsonConfig import getConfig from appPublic.dictObject import DictObject from .baseProcessor import BaseProcessor @@ -52,8 +53,8 @@ class DataSourceProcessor(BaseProcessor): async def path_call(self, request, path): dict_data = {} config = getConfig() - with codecs.open(path,'r',config.website.coding) as f: - b = f.read() + async with aiofiles.open(path,'r',encoding=config.website.coding) as f: + b = await f.read() dict_data = json.loads(b) ns = self.run_ns act = ns.get('action','getdata') diff --git a/ahserver/processorResource.py b/ahserver/processorResource.py index ae7b7fb..458cc14 100755 --- a/ahserver/processorResource.py +++ b/ahserver/processorResource.py @@ -1,6 +1,7 @@ import os import re import codecs +import aiofiles from traceback import print_exc # from showcallstack import showcallstack @@ -303,7 +304,7 @@ class ProcessorResource(AppLogger, StaticResource,Url2File): if processor: return await processor.handle(request) - if self.request_filename and self.isHtml(self.request_filename): + if self.request_filename and await self.isHtml(self.request_filename): return await self.html_handle(request, self.request_filename) if self.request_filename and os.path.isdir(self.request_filename): @@ -324,10 +325,8 @@ class ProcessorResource(AppLogger, StaticResource,Url2File): return '/'.join(str(request.url).split('/')[:3]) async def html_handle(self,request,filepath): - with open(filepath,'rb') as f: - b = f.read() - utxt = b.decode('unicode_escape') - txt = b.decode('utf-8') + async with aiofiles.open(filepath,'r', encoding='utf-8') as f: + txt = await f.read() headers = { 'Content-Type': 'text/html; utf-8', 'Accept-Ranges': 'bytes', @@ -336,10 +335,10 @@ class ProcessorResource(AppLogger, StaticResource,Url2File): resp = Response(text=txt,headers=headers) return resp - def isHtml(self,fn): + async def isHtml(self,fn): try: - with codecs.open(fn,'r','utf-8') as f: - b = f.read() + async with aiofiles.open(fn,'r',encoding='utf-8') as f: + b = await f.read() while b[0] in ['\n',' ','\t']: b = b[1:] if b.lower().startswith(''): diff --git a/ahserver/websocketProcessor.py b/ahserver/websocketProcessor.py index 74c4524..1e59b0d 100755 --- a/ahserver/websocketProcessor.py +++ b/ahserver/websocketProcessor.py @@ -1,5 +1,6 @@ import asyncio import aiohttp +import aiofiles import json import codecs from aiohttp import web @@ -43,14 +44,15 @@ class XtermProcessor(BaseProcessor): self.retResponse = ws return ws - def get_login_info(self): - with codecs.open(self.real_path, 'r', 'utf-8') as f: - self.login_info = json.load(f) + async def get_login_info(self): + async with aiofiles.open(self.real_path, 'r', encoding='utf-8') as f: + txt = await f.read() + self.login_info = json.loads(txt) print(f'{self.login_info=}') async def create_process(self): # id = lenv['params_kw'].get('termid') - self.get_login_info() + await self.get_login_info() host = self.login_info['host'] port = self.login_info.get('port', 22) username = self.login_info.get('username', 'root') diff --git a/requirements.txt b/requirements.txt index 6221d9e..842a655 100755 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ cchardet aiohttp aiohttp_session aiohttp_auth_autz +aiohttp-cors aiomysql aioredis psycopg2-binary