common_functions/functions.py

29 lines
847 B
Python
Raw Normal View History

2023-07-22 20:24:37 +00:00
import os
import shutil
2023-07-23 08:44:15 +00:00
from functools import partial
from http.server import HTTPServer, SimpleHTTPRequestHandler
from pathlib import Path
2023-07-22 20:24:37 +00:00
def read_file(file_path):
with open(file_path, 'r') as f:
file_content = f.read()
return file_content
def write_file(file_path, content):
with open(file_path, 'w') as f:
f.write(content)
def copy_file(src, dst):
os.makedirs(os.path.dirname(dst), exist_ok=True)
2023-07-23 08:44:15 +00:00
shutil.copy2(src, dst)
2023-07-23 13:11:26 +00:00
2023-07-23 08:44:15 +00:00
# Функция для запуска веб-сервера
2023-07-23 13:11:26 +00:00
def start_httpd(directory: Path, port: int = 8000, interface: str = 'localhost'):
print(f"Listen on http://{interface}:{port}, serving from '{directory}' directory...")
2023-07-23 08:44:15 +00:00
handler = partial(SimpleHTTPRequestHandler, directory=directory)
2023-07-23 13:11:26 +00:00
httpd = HTTPServer((interface, port), handler)
2023-07-23 08:44:15 +00:00
httpd.serve_forever()