From dd8dd2f3f4362e49058e8c55d2f8f8832459eed7 Mon Sep 17 00:00:00 2001 From: Gyokhan Kochmarla Date: Mon, 29 Apr 2024 00:49:45 +0300 Subject: [PATCH 1/2] fix: create session with pool connections --- core/strategies/wifi/admin_panel_strategy.py | 18 +++++++++++++----- requirements-dev.txt | 11 +++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 requirements-dev.txt diff --git a/core/strategies/wifi/admin_panel_strategy.py b/core/strategies/wifi/admin_panel_strategy.py index 3a5f42e..c36b2c3 100644 --- a/core/strategies/wifi/admin_panel_strategy.py +++ b/core/strategies/wifi/admin_panel_strategy.py @@ -4,6 +4,7 @@ from typing import Any import requests +from requests.adapters import HTTPAdapter from bs4 import BeautifulSoup from core.strategies.wifi.base_wifi_strategy import BaseWiFiStrategy @@ -23,6 +24,14 @@ 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. + http_adaptor: HTTPAdapter = HTTPAdapter( + pool_connections=1, max_retries=3 + ) + 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 +47,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 +58,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 +71,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 From 200a2d9136b0056900122e919cf79de930d21e34 Mon Sep 17 00:00:00 2001 From: Gyokhan Kochmarla Date: Mon, 29 Apr 2024 00:57:34 +0300 Subject: [PATCH 2/2] feat: add retries amount and backoff method --- core/strategies/wifi/admin_panel_strategy.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/strategies/wifi/admin_panel_strategy.py b/core/strategies/wifi/admin_panel_strategy.py index c36b2c3..8dd32a8 100644 --- a/core/strategies/wifi/admin_panel_strategy.py +++ b/core/strategies/wifi/admin_panel_strategy.py @@ -5,6 +5,7 @@ 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 @@ -27,8 +28,9 @@ def __init__(self, login_data: dict[str, Any]) -> None: 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=3 + pool_connections=1, max_retries=retries ) self._session.mount("http://", http_adaptor) self._session.mount("https://", http_adaptor)