| """ifstats_skids: gather wireless status from a skid in STA mode.""" |
| # coding: utf-8 |
| import HTMLParser |
| |
| import requests |
| |
| |
| def GetStatusWireless(bridge_ip): |
| resp = requests.get('http://{}/status_wireless.php'.format(bridge_ip)) |
| return resp.text |
| |
| |
| class MyHTMLParser(HTMLParser.HTMLParser): |
| """Extract connection information from the skid's status_wireless.php page.""" |
| |
| def __init__(self): |
| # HTMLParser is an old-style class (!!!, http://stackoverflow.com/a/9698750) |
| HTMLParser.HTMLParser.__init__(self) |
| self.in_tables = 0 |
| self.in_td = False |
| self.data = '' |
| self.dlist = [] |
| |
| def handle_starttag(self, tag, attrs): |
| if not self.in_tables: |
| if tag == 'table' and ('class', 'tablemain') in attrs: |
| self.in_tables += 1 |
| else: |
| if tag == 'table': |
| self.in_tables += 1 |
| elif tag == 'td': |
| self.in_td = True |
| |
| def handle_endtag(self, tag): |
| if self.in_tables: |
| if tag == 'table': |
| self.in_tables -= 1 |
| elif tag == 'td': |
| self.in_td = False |
| self.dlist += [self.data] |
| self.data = '' |
| |
| def handle_data(self, data): |
| if self.in_tables: |
| self.data += data |
| |
| |
| def ParseStatusWireless(text): |
| """Parse a status_wireless.php page returned from a skid.""" |
| parser = MyHTMLParser() |
| parser.feed(text) |
| |
| results = {} |
| key = '' |
| for cell in [item.strip() for item in parser.dlist][3:-1]: |
| if not key: |
| key = cell[:-1] |
| continue |
| |
| results[key] = cell |
| key = '' |
| |
| return results |
| |
| |
| if __name__ == '__main__': |
| out = GetStatusWireless('192.168.1.167') |
| print ParseStatusWireless(out) |
| |