diff --git a/core/strategies/wifi/admin_panel_strategy.py b/core/strategies/wifi/admin_panel_strategy.py index 3a5f42e..8dd32a8 100644 --- a/core/strategies/wifi/admin_panel_strategy.py +++ b/core/strategies/wifi/admin_panel_strategy.py @@ -4,6 +4,8 @@ from typing import Any import requests +from requests.adapters import HTTPAdapter +from urllib3.util.retry import Retry from bs4 import BeautifulSoup from core.strategies.wifi.base_wifi_strategy import BaseWiFiStrategy @@ -23,6 +25,15 @@ def __init__(self, login_data: dict[str, Any]) -> None: """Constructor for AdminPanelStrategy.""" super().__init__() self._login_data: dict[str, Any] = login_data + self._session: requests.Session = requests.Session() + + # Create a HTTP adapter to limit the number of connections. + retries = Retry(total=3, backoff_factor=0.3, backoff_max=5.0) + http_adaptor: HTTPAdapter = HTTPAdapter( + pool_connections=1, max_retries=retries + ) + self._session.mount("http://", http_adaptor) + self._session.mount("https://", http_adaptor) def check_protectors(self) -> WiFiStrategyResult: """This method checks if there are any protectors around.""" @@ -38,9 +49,8 @@ def check_protectors(self) -> WiFiStrategyResult: def _get_all_connected(self) -> list[ConnectedDeviceResult]: """This method returns a list of addresses of the clients connected to the network.""" # Create a session to store cookies. - session = requests.Session() - session.get("http://192.168.1.95/login_security.html") - session.post( + self._session.get("http://192.168.1.95/login_security.html") + self._session.post( "http://192.168.1.95/Forms/login_security_1", headers={ "Content-Type": "application/x-www-form-urlencoded", @@ -50,7 +60,8 @@ def _get_all_connected(self) -> list[ConnectedDeviceResult]: ) # Get the response for the page with MAC address list. - response = session.get("http://192.168.1.95/status/status_deviceinfo.htm") + response = self._session.get( + "http://192.168.1.95/status/status_deviceinfo.htm") # Parse the response. soup = BeautifulSoup(response.text, "html.parser") @@ -62,7 +73,6 @@ def _get_all_connected(self) -> list[ConnectedDeviceResult]: for element in tabdata if len(element.text) == 17 ] - session.close() logger.debug("Connected devices: %s", str(mac_addrs)) return [ConnectedDeviceResult(mac_addr.upper()) for mac_addr in mac_addrs] diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..d79125a --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,11 @@ +astroid==3.1.0 +dill==0.3.8 +flake8==7.0.0 +Flake8-pyproject==1.2.3 +isort==5.13.2 +mccabe==0.7.0 +platformdirs==4.2.1 +pycodestyle==2.11.1 +pyflakes==3.2.0 +pylint==3.1.0 +tomlkit==0.12.4