Skip to content

Commit d910da4

Browse files
fix: Too many open files (#16)
* fix: create session with pool connections * feat: add retries amount and backoff method
1 parent c37a930 commit d910da4

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

core/strategies/wifi/admin_panel_strategy.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from typing import Any
55

66
import requests
7+
from requests.adapters import HTTPAdapter
8+
from urllib3.util.retry import Retry
79
from bs4 import BeautifulSoup
810

911
from core.strategies.wifi.base_wifi_strategy import BaseWiFiStrategy
@@ -23,6 +25,15 @@ def __init__(self, login_data: dict[str, Any]) -> None:
2325
"""Constructor for AdminPanelStrategy."""
2426
super().__init__()
2527
self._login_data: dict[str, Any] = login_data
28+
self._session: requests.Session = requests.Session()
29+
30+
# Create a HTTP adapter to limit the number of connections.
31+
retries = Retry(total=3, backoff_factor=0.3, backoff_max=5.0)
32+
http_adaptor: HTTPAdapter = HTTPAdapter(
33+
pool_connections=1, max_retries=retries
34+
)
35+
self._session.mount("http://", http_adaptor)
36+
self._session.mount("https://", http_adaptor)
2637

2738
def check_protectors(self) -> WiFiStrategyResult:
2839
"""This method checks if there are any protectors around."""
@@ -38,9 +49,8 @@ def check_protectors(self) -> WiFiStrategyResult:
3849
def _get_all_connected(self) -> list[ConnectedDeviceResult]:
3950
"""This method returns a list of addresses of the clients connected to the network."""
4051
# Create a session to store cookies.
41-
session = requests.Session()
42-
session.get("http://192.168.1.95/login_security.html")
43-
session.post(
52+
self._session.get("http://192.168.1.95/login_security.html")
53+
self._session.post(
4454
"http://192.168.1.95/Forms/login_security_1",
4555
headers={
4656
"Content-Type": "application/x-www-form-urlencoded",
@@ -50,7 +60,8 @@ def _get_all_connected(self) -> list[ConnectedDeviceResult]:
5060
)
5161

5262
# Get the response for the page with MAC address list.
53-
response = session.get("http://192.168.1.95/status/status_deviceinfo.htm")
63+
response = self._session.get(
64+
"http://192.168.1.95/status/status_deviceinfo.htm")
5465

5566
# Parse the response.
5667
soup = BeautifulSoup(response.text, "html.parser")
@@ -62,7 +73,6 @@ def _get_all_connected(self) -> list[ConnectedDeviceResult]:
6273
for element in tabdata
6374
if len(element.text) == 17
6475
]
65-
session.close()
6676

6777
logger.debug("Connected devices: %s", str(mac_addrs))
6878
return [ConnectedDeviceResult(mac_addr.upper()) for mac_addr in mac_addrs]

requirements-dev.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
astroid==3.1.0
2+
dill==0.3.8
3+
flake8==7.0.0
4+
Flake8-pyproject==1.2.3
5+
isort==5.13.2
6+
mccabe==0.7.0
7+
platformdirs==4.2.1
8+
pycodestyle==2.11.1
9+
pyflakes==3.2.0
10+
pylint==3.1.0
11+
tomlkit==0.12.4

0 commit comments

Comments
 (0)