-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
76 lines (60 loc) · 2.57 KB
/
server.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from asyncio import CancelledError
from pathlib import Path
import asyncio
from aiohttp import web
import aiofiles
from utils import create_logger, create_parser
from settings import DEFAULT_BYTES_FOR_READ, INTERVAL_SECS, MEDIA_DIR
parser = create_parser()
logger = create_logger()
async def handle_index_page(request):
async with aiofiles.open('index.html', mode='r') as index_file:
index_contents = await index_file.read()
return web.Response(text=index_contents, content_type='text/html')
async def archivate(request):
response = web.StreamResponse()
# Большинство браузеров не отрисовывают частично загруженный контент, только если это не HTML.
# Поэтому отправляем клиенту именно HTML, указываем это в Content-Type.
archive_hash = request.match_info['archive_hash']
response.headers['Content-Disposition'] = f'filename = "{archive_hash}.zip"'
path_to_archive = Path(app['media_dir'], f'{archive_hash}')
if not path_to_archive.exists() or not path_to_archive.is_dir():
raise web.HTTPNotFound()
cmd = 'zip', '-r', '-', '.',
proc = await asyncio.create_subprocess_exec(
*cmd,
cwd=path_to_archive.as_posix(),
stdout=asyncio.subprocess.PIPE,)
# Отправляет клиенту HTTP заголовки
await response.prepare(request)
response.enable_chunked_encoding()
stdout = proc.stdout
try:
while not stdout.at_eof():
logger.debug('start read bytes')
stdout_bytes = await stdout.read(DEFAULT_BYTES_FOR_READ)
# Отправляет клиенту очередную порцию ответа
logger.debug(f'[Sending archive chunk ... ] {len(stdout_bytes)}')
await response.write(stdout_bytes)
if INTERVAL_SECS:
await asyncio.sleep(INTERVAL_SECS)
return response
except CancelledError:
logger.debug('Download was interrupted')
raise
except Exception as e:
logger.debug(f'Unexpected exception caught {e}', exc_info=True)
raise
finally:
if proc.returncode is None:
proc.kill()
outs, errs = await proc.communicate()
if __name__ == '__main__':
app = web.Application()
app['media_dir'] = MEDIA_DIR
app.add_routes([
web.get('/archive/{archive_hash}/', archivate),
web.get('/', handle_index_page),
])
args = parser.parse_args()
web.run_app(app, path=args.path, port=args.port)