def get_wireless_settings(self): html_initial = self._make_http_request_read('WLG_wireless.htm') match_object = re.compile('var authType="(.+?)";').search(html_initial) if match_object is None: raise RouterFetchError( 'Cannot parse initial wireless settings/authType') auth_type = match_object.group(1) match_object = re.compile('var wepStatus="(.+?)";').search( html_initial) if match_object is None: raise RouterFetchError( 'Cannot parse initial wireless settings/wepStatus') wep_status = match_object.group(1) if wep_status == 'Enable': if auth_type in ('WPA-PSK', 'WPA2-PSK', 'WPA-AUTO-PSK'): doc_name = 'WLG_wireless2.htm' else: auth_type = 'WEP' # I've seen auth_type='None' to mean 'WEP'.. weird.. doc_name = 'WLG_wireless1.htm' else: doc_name = 'WLG_wireless3.htm' args = [auth_type] args.append(self._make_http_request_read(doc_name)) args.append(self._make_http_request_read('WLG_adv.htm')) settings = _parse_wireless_settings(*args) # Only v7 doesn't support auto channel settings.set_auto_channel_support(False) return settings
def _perform_http_request(self, url, data=None, headers=(), timeout=7.0): """Makes the actual HTTP request and returns the result. The result is a 3-tuple: - requested URL - info() - meta information, such as headers - contents """ if self._is_first_request: self._is_first_request = False self._handle_first_request() if data is not None: if isinstance(data, dict) or isinstance(data, list): data = urlencoder.urlencode(data) else: raise RouterFetchError( 'POST data should be a dict, a list or None!' ) try: req = requestor.Request(url, data) for header, value in headers: req.add_header(header, value) with contextlib.closing(requestor.urlopen(req, timeout=timeout)) as handle: self._is_logged_in = True return ( handle.geturl(), handle.info(), handle.read().decode('utf-8', 'ignore') ) except Exception as e: raise RouterFetchError('Failed making request: %s' % repr(e))
def _handle_first_request(self): """We need to make an initial "login" request. The router will respond with 401 Unauthorized (RouterFetchError) to other requests if we start making requests directly. What we're doing here is making a request which catches that initial 401 error and simply ignores it. This function also does something else special for Netgear routers. The web management interface can be used from only one IP at a time. Access to the router is locked until the user logs out (handled in :meth:`close`) or his session expires. If the router replies with a non-401 response on our first request, it may be telling us that someone else is logged in and managing it. """ try: contents = self._make_http_request_read('RST_status.htm') except RouterFetchError: # it's quite normal if we're here! pass else: # We got a good response, that's somewhat abnormal # It could mean that someone else is managing the device # Or that we're already "logged in" somehow if ') is managing this device' in contents: regex = re.compile('\((.+?)\) is managing this device') match_obj = regex.search(contents) ip = match_obj.group(1) if match_obj is not None else 'unknown' raise RouterFetchError('Device is managed by %s!' % ip)
def get_router_info(self): search_object = self.connect() if not search_object: raise RouterFetchError("< HTTPError 401: 'N/A' >") setattr(Tplink_WR740N_, 'url_base', 'http://{0}:{1}'.format(self.host, self.port) + '/' + search_object[0] + '/userRpm/') return super().get_router_info()
def get_wireless_settings(self): # Maps a security type with the document id, which contains its settings security_types_map = {'None': 3, 'WEP': 1, 'WPA-PSK': 2, 'WPA2-PSK': 2, 'WPA-AUTO-PSK': 2} html_initial = self._make_http_request_read('WLG_wireless.htm') match_object = re.compile('var secuType="(%s)";' % '|'.join(security_types_map)).search(html_initial) if match_object is None: raise RouterFetchError('Cannot parse initial wireless settings') doc_name = 'WLG_wireless%d.htm' % security_types_map[match_object.group(1)] args = [match_object.group(1)] args.append(self._make_http_request_read(doc_name)) args.append(self._make_http_request_read('WLG_adv.htm')) return _parse_wireless_settings(*args)