From 07a1af133bbd35624f54f237cb5e2df6f6debe07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Thu, 9 Jun 2022 14:33:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=87=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=B1=D0=B5=D1=81=D0=BF=D0=BE=D0=BB=D0=B5=D0=B7?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=BD=D0=B0=20aiohttp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solution.py | 71 ++++++++++++++++++++++------------------------------- 1 file changed, 29 insertions(+), 42 deletions(-) diff --git a/solution.py b/solution.py index 1aa91cb..4be34d5 100644 --- a/solution.py +++ b/solution.py @@ -4,51 +4,49 @@ import json from typing import List import os import configparser +import aiohttp -async def write_config_file( - server_name: str, path_to_template_file: str, path_for_config: str +async def write_config_files( + servers: list, path_to_template_file: str, path_for_config: str ) -> None: template: str = "" with open(path_to_template_file, "r") as file: template = file.read() - config_body: str = template.replace( - "server_name _;", f"server_name {server_name}.server.com;" - ) - condifg_full_path: str = os.path.abspath(path_for_config) - config_filename: str = f"{server_name}.conf" + config_full_path: str = os.path.abspath(path_for_config) + if not os.path.isdir(config_full_path): + os.mkdir(config_full_path) - if not os.path.isdir(condifg_full_path): - os.mkdir(condifg_full_path) + for server in servers: - with open(os.path.join(condifg_full_path, config_filename), "w") as file: - file.write(config_body) - - -async def send_request(server: str, columns: list, limit: int = 1) -> dict: - response = requests.get(server, json={"columns": columns, "limit": limit}) - - return response.json() + config_body: str = template.replace( + "server_name _;", f"server_name {server}.server.com;" + ) + config_filename: str = f"{server}.conf" + with open(os.path.join(config_full_path, config_filename), "w") as file: + file.write(config_body) -async def get_hosts(server_response: dict) -> List[str]: - """Получить хосты из ответа сервера. - Parameters - ---------- - server_response : dict - """ +async def send_async_request(cfg: dict, columns: list, limit: int = 1) -> None: + body = {"columns": columns, "limit": 1} + server = cfg["central_host_url"] + path_to_template_file = cfg["template"] + path_for_config = cfg["path_for_config"] - hosts: list = [] + async with aiohttp.ClientSession() as session: + tasks = [session.get(server, json=body) for i in range(limit)] + responses = await asyncio.gather(*tasks) - for host in server_response.get("result"): - hosts.append(host.get("hostname")) + for response in responses: + r = await response.json() + hosts = [i["hostname"] for i in r.get("result")] - return hosts + await write_config_files(hosts, path_to_template_file, path_for_config) -async def read_config(path_to_conf_file: str, section: str = "Main") -> dict: +def read_config(path_to_conf_file: str, section: str = "Main") -> dict: """ Считать конфиг с помощью `configparser`. @@ -79,25 +77,14 @@ async def read_config(path_to_conf_file: str, section: str = "Main") -> dict: return dict(config.items(section)) -async def main(): +async def main() -> None: - cnf = await read_config("service.conf") + cnf = read_config("service.conf") wait_sec: int = int(cnf["frequency_sec"]) while True: - resp = await send_request( - cnf["central_host_url"], columns=["hostname"], limit=9 - ) - - hosts = await get_hosts(resp) - - for host in hosts: - await write_config_file( - server_name=host, - path_to_template_file=cnf["template"], - path_for_config=cnf["path_for_config"], - ) + await send_async_request(cnf, columns=["hostname"], limit=9999) await asyncio.sleep(wait_sec)