Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
RafayGhafoor committed Mar 16, 2018
1 parent 91a6f7f commit de07e7a
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions router.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'''
A PTCL router class, which allows basic functionality for PTCL router.
A PTCL router class which allows basic functionality for PTCL router.
Usage Example:
# router is used an instance of Router class in all examples.
# router is used as an instance for the Router class in all examples.
>>> from router import Router
>>> router = Router('192.168.1.1') # Connects session for interacting with router
>>> router = Router(gateway='192.168.1.1') # Launches session for interacting with router
>>>
>>> router.reboot() # Reboots router
>>> router.stationinfo() # Returns a list of active devices
Expand Down Expand Up @@ -40,7 +40,7 @@ def __init__(self, gateway="192.168.10.1", username="admin", password="admin"):
self.active_dev = [] # Active Devices on Wi-Fi
self.session = requests.Session()
self.session.auth = (self.username, self.password)
self.sessionKey = ""
self.sessionKey = None


def scrape_page(self, url='', params='', soup='n'):
Expand All @@ -51,6 +51,7 @@ def scrape_page(self, url='', params='', soup='n'):
'''
if not url:
return

try:
request_url = self.session.get(url, params=params)
if request_url.status_code == 401:
Expand All @@ -60,6 +61,7 @@ def scrape_page(self, url='', params='', soup='n'):
html_soup = bs4.BeautifulSoup(request_url.content, 'lxml')
return html_soup
return request_url

except requests.exceptions.ConnectionError:
print("Internet Connection Down.\nExiting...")
sys.exit()
Expand Down Expand Up @@ -87,24 +89,26 @@ def dhcpinfo(self):
Return:
self.dev_info (Dictionary object)
'''
soup = self.scrape_page(url=self.gateway + "dhcpinfo.html", soup='y')
soup = self.scrape_page(url=(self.gateway + "dhcpinfo.html"), soup='y')
td = soup.findAll('td')
for i in td:
if self.mac_pattern.search(i.text):
'''
The HTML page contains hostnames and mac addresses right next
to each other in the form of table. We search in the tables list
(td) until a mac address is found, then appends it to the
mac_address list. The hostname is located before it so by using
index less than the current index of mac address, we obtain the
hostname and append it to the dev_hostname list.
'''
# Before mac_addresses, there is hostname
# After mac_addresses, there are local ip and expire time for
# the device connected
hostname = td[td.index(i) - 1].text
self.dev_info["Hostname"] = hostname
self.dev_info[hostname] = [i.text, td[td.index(i) + 1].text, td[td.index(i) + 2].text]
if not self.mac_pattern.search(i.text):
return
'''
The HTML page contains hostnames and mac addresses right next
to each other in the form of table. We search in the tables list
(td) until a mac address is found, then appends it to the
mac_address list. The hostname is located before it so by using
index less than the current index of mac address, we obtain the
hostname and append it to the dev_hostname list.
'''
# Before mac_addresses, there is hostname
# After mac_addresses, there are local ip and expire time for
# the devices connected
hostname = td[td.index(i) - 1].text
self.dev_info["Hostname"] = hostname
self.dev_info[hostname] = [i.text, td[td.index(i) + 1].text, td[td.index(i) + 2].text]

return self.dev_info


Expand Down

0 comments on commit de07e7a

Please sign in to comment.