def set_proxy(proxy, user=None, password=""): """ Set the HTTP proxy for Python to download through. If ``proxy`` is None then tries to set proxy from environment or system settings. :param proxy: The HTTP proxy server to use. For example: 'http://proxy.example.com:3128/' :param user: The username to authenticate with. Use None to disable authentication. :param password: The password to authenticate with. """ from nltk import compat if proxy is None: # Try and find the system proxy settings try: proxy = getproxies()["http"] except KeyError: raise ValueError("Could not detect default proxy settings") # Set up the proxy handler proxy_handler = ProxyHandler({"https": proxy, "http": proxy}) opener = build_opener(proxy_handler) if user is not None: # Set up basic proxy authentication if provided password_manager = HTTPPasswordMgrWithDefaultRealm() password_manager.add_password(realm=None, uri=proxy, user=user, passwd=password) opener.add_handler(ProxyBasicAuthHandler(password_manager)) opener.add_handler(ProxyDigestAuthHandler(password_manager)) # Overide the existing url opener install_opener(opener)
def setup_opener(self, url, timeout): """ Sets up a urllib OpenerDirector to be used for requests. There is a fair amount of custom urllib code in Package Control, and part of it is to handle proxies and keep-alives. Creating an opener the way below is because the handlers have been customized to send the "Connection: Keep-Alive" header and hold onto connections so they can be re-used. :param url: The URL to download :param timeout: The int number of seconds to set the timeout to """ if not self.opener: http_proxy = self.settings.get('http_proxy') https_proxy = self.settings.get('https_proxy') if http_proxy or https_proxy: proxies = {} if http_proxy: proxies['http'] = http_proxy if https_proxy: proxies['https'] = https_proxy proxy_handler = ProxyHandler(proxies) else: proxy_handler = ProxyHandler() password_manager = HTTPPasswordMgrWithDefaultRealm() proxy_username = self.settings.get('proxy_username') proxy_password = self.settings.get('proxy_password') if proxy_username and proxy_password: if http_proxy: password_manager.add_password(None, http_proxy, proxy_username, proxy_password) if https_proxy: password_manager.add_password(None, https_proxy, proxy_username, proxy_password) handlers = [proxy_handler] basic_auth_handler = ProxyBasicAuthHandler(password_manager) digest_auth_handler = ProxyDigestAuthHandler(password_manager) handlers.extend([digest_auth_handler, basic_auth_handler]) debug = self.settings.get('debug') if debug: console_write(u"Urllib Debug Proxy", True) console_write(u" http_proxy: %s" % http_proxy) console_write(u" https_proxy: %s" % https_proxy) console_write(u" proxy_username: %s" % proxy_username) console_write(u" proxy_password: %s" % proxy_password) secure_url_match = re.match('^https://([^/]+)', url) if secure_url_match != None: secure_domain = secure_url_match.group(1) bundle_path = self.check_certs(secure_domain, timeout) bundle_path = bundle_path.encode(sys.getfilesystemencoding()) handlers.append( ValidatingHTTPSHandler( ca_certs=bundle_path, debug=debug, passwd=password_manager, user_agent=self.settings.get('user_agent'))) else: handlers.append( DebuggableHTTPHandler(debug=debug, passwd=password_manager)) self.opener = build_opener(*handlers)