44from typing import Any
55
66import requests
7+ from requests .adapters import HTTPAdapter
8+ from urllib3 .util .retry import Retry
79from bs4 import BeautifulSoup
810
911from 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 ]
0 commit comments