def get_parsed_proxy(typ='http', debug=True): proxies = get_proxies(debug) proxy = proxies.get(typ, None) if proxy: pattern = re.compile(('(?:ptype://)?' '(?:(?P<user>\\w+):(?P<pass>.*)@)?' '(?P<host>[\\w\\-\\.]+)' '(?::(?P<port>\\d+))?').replace('ptype', typ)) match = pattern.match(proxies[typ]) if match: try: ans = { 'host': match.group('host'), 'port': match.group('port'), 'user': match.group('user'), 'pass': match.group('pass') } if ans['port']: ans['port'] = int(ans['port']) except: if debug: import traceback traceback.print_exc() else: if debug: prints('Using http proxy', unicode_type(ans)) return ans
def get_proxies(debug=True): from polyglot.urllib import getproxies proxies = getproxies() for key, proxy in list(proxies.items()): if not proxy or '..' in proxy or key == 'auto': del proxies[key] continue if proxy.startswith(key + '://'): proxy = proxy[len(key) + 3:] if key == 'https' and proxy.startswith('http://'): proxy = proxy[7:] if proxy.endswith('/'): proxy = proxy[:-1] if len(proxy) > 4: proxies[key] = proxy else: prints('Removing invalid', key, 'proxy:', proxy) del proxies[key] if proxies and debug: prints('Using proxies:', proxies) return proxies
def _prints(self, *args, **kwargs): prints(*args, **kwargs, file=self.stream)