Python
0
Вклад в тег
from aiohttp import web, ClientSession
class ProxyHandler(web.View):
BAD_REQUEST_HEADERS = ['host', 'accept-encoding']
BAD_RESPONSE_HEADERS = ['content-encoding', 'transfer-encoding']
async def get(self):
url = request.GET.get('url')
request_headers = []
# убиваем "вредные" залоговки запроса
for key, value in request.headers.items():
if key.lower() in self.BAD_REQUEST_HEADERS:
continue
response_headers.append(key, value)
with ClientSession() as session:
async with session.get(url, headers=request_headers) as response:
response_headers = []
# убиваем "вредные" залоговки ответа
for item in response.headers:
key, value = item
if key.lower() in self.BAD_RESPONSE_HEADERS:
continue
response_headers.append((key, value))
body = await response.read()
return web.Response(body=body, headers=response_headers, status=response.status)