ahserver_g/ahserver/proxyProcessor.py

46 lines
1.1 KiB
Python
Raw Normal View History

2022-03-21 16:30:00 +08:00
from aiohttp import web
from aiohttp import client
from .baseProcessor import *
2022-03-22 11:45:21 +08:00
class ProxyProcessor(BaseProcessor):
2022-03-21 16:30:00 +08:00
@classmethod
def isMe(self,name):
return name=='proxy'
async def path_call(self, request, params={}):
await self.set_run_env(request)
path = self.path
url = self.resource.entireUrl(request, path)
ns = self.run_ns
ns.update(params)
te = self.run_ns['tmpl_engine']
txt = await te.render(url,**ns)
data = json.loads(txt)
2022-03-21 16:41:03 +08:00
print('proxyProcessor: data=', data)
2022-03-21 16:30:00 +08:00
return data
async def datahandle(self,request):
d = await self.path_call(request)
reqH = request.headers.copy()
async with client.request(
request.method,
d['url'],
headers = reqH,
allow_redirects=False,
2022-03-22 11:50:47 +08:00
data=await request.read()) as res:
2022-03-21 16:30:00 +08:00
headers = res.headers.copy()
2022-03-22 12:18:18 +08:00
# body = await res.read()
2022-03-21 16:30:00 +08:00
self.retResponse = web.Response(
headers = headers,
2022-03-22 12:18:18 +08:00
status = res.status
# , body=body
2022-03-21 16:30:00 +08:00
)
2022-03-22 12:18:18 +08:00
async for chunk in res.content.iter_chunked(chunk_size):
await self.retResponse.write(chunk)
2022-03-21 16:41:03 +08:00
print('proxy: datahandle() finish', self.retResponse)
2022-03-21 16:30:00 +08:00
def setheaders(self):
pass