-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.py
More file actions
54 lines (37 loc) · 2.09 KB
/
Copy pathServer.py
File metadata and controls
54 lines (37 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from aiohttp import web, ClientConnectorError, ClientResponseError, ClientSession, BasicAuth
import logging
from aiohttp_client_cache import CachedSession, SQLiteBackend
from datetime import timedelta
import ssl
logging.basicConfig(level=logging.INFO, filename='app.log', filemode='w',
format='%(name)s - %(levelname)s - %(message)s')
cache = SQLiteBackend(expire_after=timedelta(days=30))
async def handle(request):
try:
logging.info("Get authentication")
auth_header = request.headers.get('Proxy-Authorization')
if not auth_header:
return web.Response(status=407, headers={'Proxy-Authenticate': 'Basic realm="Proxy Authentication"'},
text='Proxy Authentication Required')
auth = BasicAuth.decode(auth_header)
if auth.login != 'admin_login' or auth.password != 'admin_password':
logging.info("login or password incorrect")
return web.Response(status=407, headers={'Proxy-Authenticate': 'Basic realm="Proxy Authentication"'},
text='Proxy Authentication Required')
logging.info(f'Handling request for {request.url}')
async with CachedSession(cache=cache) as session:
async with session.get(request.url, ssl=ssl.create_default_context()) as resp:
logging.info(f'Response received for {request.url}')
response = web.StreamResponse()
response.content_type = resp.content_type
response.charset = resp.charset
await response.prepare(request)
async for data in resp.content.iter_any():
await response.write(data)
return response
except (BaseException, ClientConnectorError, ClientResponseError, UnicodeDecodeError, ConnectionResetError) as e:
logging.error(f'Error occurred while handling request for {request.url}: {str(e)}')
return web.Response(text=str(e), status=500)
app = web.Application()
app.router.add_route('*', '/{path:.*}', handle)
web.run_app(app, host='127.0.0.1', port=8080)