analyze this script part by part then, rate it from 1 to 10 (ignore ssl insecure enable,…
- User
analyze this script part by part then, rate it from 1 to 10 (ignore ssl insecure enable, hardcoded user and password, they are implemented that way because limitations)
import argparse import asyncio import json import logging import signal import ssl import sys import time from datetime import datetime from typing import Dict, List, Optional, Tuple
import aiohttp import colorlog import netifaces from aiohttp import ClientError, ClientResponseError, ClientConnectorError, web from bs4 import BeautifulSoup
Configuration settings
CONFIG = { 'base_url': 'https://10.0.0.2', 'username': 'misael_campos', 'password': 'd337O8AetR3p4', 'post_endpoint': '/action/pononuopticalinfo.html', 'who': 100, 'onuid': 0, 'pon_range': range(1, 17), 'onu_group_range': range(0, 2), 'request_timeout': aiohttp.ClientTimeout( total=240, # Total operation timeout connect=120, # Connection timeout sock_read=120 # Socket read timeout ), 'max_concurrent_requests': 8, # Reduced to prevent server overload 'max_retries': 3, # Maximum number of retry attempts 'retry_delay': 1, # Delay between retries in seconds 'batch_size': 8, # Number of concurrent requests per batch 'host': '0.0.0.0', # Host address 'port': 8080, # Port number 'endpoint': '/data', # Added endpoint path 'ignore_row_length_warning': False # New configuration option }
Configure logging
handler = colorlog.StreamHandler() handler.setFormatter(colorlog.ColoredFormatter( '%(log_color)s%(asctime)s [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S', log_colors={ 'DEBUG': 'light_cyan', 'INFO': 'light_green', 'WARNING': 'light_yellow', 'ERROR': 'light_red', 'CRITICAL': 'light_red,bg_white', } ))
logger = colorlog.getLogger(name) logger.addHandler(handler) logger.setLevel(logging.INFO)
Remove any existing handlers to avoid duplicate logs
for hdlr in logger.handlers[:-1]: logger.removeHandler(hdlr)
def get_all_ip_addresses() -> List[str]: """ Retrieve all non-loopback IPv4 addresses from the machine's network interfaces.
codeReturns: List[str]: A list of IPv4 addresses. """ ips = [] try: interfaces = netifaces.interfaces() for iface in interfaces: addrs = netifaces.ifaddresses(iface) ipv4 = addrs.get(netifaces.AF_INET) if ipv4: for addr in ipv4: ip = addr.get('addr') if ip and ip != '127.0.0.1': ips.append(ip) except ValueError as ve: logger.error(f"Error retrieving IP addresses: {ve}") except Exception as e: logger.error(f"Unexpected error retrieving IP addresses: {e}") return ips - Assistant