ebat obnova
This commit is contained in:
37
main.py
37
main.py
@@ -5,6 +5,8 @@ import datetime
|
||||
import aiofiles
|
||||
import asyncio
|
||||
import config
|
||||
import utils
|
||||
import time
|
||||
import os
|
||||
|
||||
STATUS = {
|
||||
@@ -60,9 +62,10 @@ class WebServer:
|
||||
async def handle(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
|
||||
addr = writer.get_extra_info("peername")
|
||||
await self.log(config.conn_msg, addr)
|
||||
conn_time = time.time()
|
||||
|
||||
rdata = await reader.read(config.read_buffer)
|
||||
data = rdata.decode()
|
||||
|
||||
if not data: return
|
||||
|
||||
real_addr = None
|
||||
@@ -110,9 +113,10 @@ class WebServer:
|
||||
|
||||
files = ""
|
||||
base_path = os.path.relpath(file_path, config.directory).replace('\\', '/')
|
||||
if base_path == '.':
|
||||
base_path = ''
|
||||
if base_path == '.': base_path = ''
|
||||
|
||||
file_count = 0
|
||||
dir_count = 0
|
||||
for item in sorted(os.listdir(file_path)):
|
||||
item_path = os.path.join(file_path, item)
|
||||
is_dir = os.path.isdir(item_path)
|
||||
@@ -123,16 +127,29 @@ class WebServer:
|
||||
if is_dir:
|
||||
rel_path += "/"
|
||||
item += "/"
|
||||
|
||||
modify_time = os.path.getmtime(item_path)
|
||||
modify_datetime = datetime.datetime.fromtimestamp(modify_time)
|
||||
formatted_time = modify_datetime.strftime("%d.%m.%Y %H:%M:%S")
|
||||
|
||||
dir_count += 1
|
||||
else: file_count += 1
|
||||
|
||||
rel_path_encoded = rel_path.replace(' ', '%20').replace('#', '%23')
|
||||
|
||||
files += f'<a class={"dir" if is_dir else "file"} href="/{rel_path_encoded}">{item}</a> | {formatted_time}<br>\n'
|
||||
|
||||
entry = config.dir_entry if is_dir else config.file_entry
|
||||
entry = entry.replace("<NAME>", item)
|
||||
entry = entry.replace("<REL_PATH>", rel_path_encoded)
|
||||
entry = entry.replace("<CDATE>", utils.get_create_time(item_path, config.time_format))
|
||||
entry = entry.replace("<MDATE>", utils.get_mod_time(item_path, config.time_format))
|
||||
entry = entry.replace("<SIZE_B>", f"{os.path.getsize(item_path)}B")
|
||||
entry = entry.replace("<SIZE_KB>", f"{format(os.path.getsize(item_path) / 1024, ".2f")}KB")
|
||||
entry = entry.replace("<SIZE_MB>", f"{format(os.path.getsize(item_path) / 1024 ** 2, ".2f")}MB")
|
||||
|
||||
files += entry
|
||||
|
||||
resp = resp.replace("<FILES>", files)
|
||||
resp = resp.replace("<FILE_COUNT>", str(file_count))
|
||||
resp = resp.replace("<DIR_COUNT>", str(dir_count))
|
||||
resp = resp.replace("<TOTAL_COUNT>", str(file_count + dir_count))
|
||||
resp = resp.replace("<SERVER>", config.name)
|
||||
resp = resp.replace("<LOAD_TIME>", format(time.time() - conn_time, ".3f"))
|
||||
resp = resp.replace("<SERVER_TIME>", datetime.datetime.now().strftime(config.time_format))
|
||||
resp = resp.encode()
|
||||
|
||||
await self.send_headers(writer, 200, len(resp))
|
||||
|
Reference in New Issue
Block a user