Python urllib2.ProxyBasicAuthHandler() Examples

The following are 10 code examples of urllib2.ProxyBasicAuthHandler(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module urllib2 , or try the search function .
Example #1
Source File: connection.py    From plugin.video.ustvvod with GNU General Public License v2.0 6 votes vote down vote up
def prepare_us_proxy(cookie_handler):
	if (addon.getSetting('us_proxy_socks5') == 'true'):
		if ((addon.getSetting('us_proxy_pass') is not '') and (addon.getSetting('us_proxy_user') is not '')):
			print 'Using socks5 authenticated proxy: ' + addon.getSetting('us_proxy') + ':' + addon.getSetting('us_proxy_port')
			socks_handler = SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, addon.getSetting('us_proxy'), int(addon.getSetting('us_proxy_port')), True, addon.getSetting('us_proxy_user'), addon.getSetting('us_proxy_pass'))
			opener = urllib2.build_opener(socks_handler, cookie_handler)
		else:
			print 'Using socks5 proxy: ' + addon.getSetting('us_proxy') + ':' + addon.getSetting('us_proxy_port')
			socks_handler = SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, addon.getSetting('us_proxy'), int(addon.getSetting('us_proxy_port')), True)
			opener = urllib2.build_opener(socks_handler, cookie_handler)
	elif (addon.getSetting('us_proxy_socks5') == 'false'):
		us_proxy = 'http://' + addon.getSetting('us_proxy') + ':' + addon.getSetting('us_proxy_port')
		proxy_handler = urllib2.ProxyHandler({'http' : us_proxy})
		if ((addon.getSetting('us_proxy_pass') is not '') and (addon.getSetting('us_proxy_user') is not '')):
			print 'Using authenticated proxy: ' + us_proxy
			password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
			password_mgr.add_password(None, us_proxy, addon.getSetting('us_proxy_user'), addon.getSetting('us_proxy_pass'))
			proxy_auth_handler = urllib2.ProxyBasicAuthHandler(password_mgr)
			opener = urllib2.build_opener(proxy_handler, proxy_auth_handler, cookie_handler)
		else:
			print 'Using proxy: ' + us_proxy
			opener = urllib2.build_opener(proxy_handler, cookie_handler)
	return opener 
Example #2
Source File: test_urllib2.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_proxy_basic_auth(self):
        opener = OpenerDirector()
        ph = urllib2.ProxyHandler(dict(http="proxy.example.com:3128"))
        opener.add_handler(ph)
        password_manager = MockPasswordManager()
        auth_handler = urllib2.ProxyBasicAuthHandler(password_manager)
        realm = "ACME Networks"
        http_handler = MockHTTPHandler(
            407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
        opener.add_handler(auth_handler)
        opener.add_handler(http_handler)
        self._test_basic_auth(opener, auth_handler, "Proxy-authorization",
                              realm, http_handler, password_manager,
                              "http://acme.example.com:3128/protected",
                              "proxy.example.com:3128",
                              ) 
Example #3
Source File: test_urllib2.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_proxy_basic_auth(self):
        opener = OpenerDirector()
        ph = urllib2.ProxyHandler(dict(http="proxy.example.com:3128"))
        opener.add_handler(ph)
        password_manager = MockPasswordManager()
        auth_handler = urllib2.ProxyBasicAuthHandler(password_manager)
        realm = "ACME Networks"
        http_handler = MockHTTPHandler(
            407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
        opener.add_handler(auth_handler)
        opener.add_handler(http_handler)
        self._test_basic_auth(opener, auth_handler, "Proxy-authorization",
                              realm, http_handler, password_manager,
                              "http://acme.example.com:3128/protected",
                              "proxy.example.com:3128",
                              ) 
Example #4
Source File: test_urllib2.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_proxy_basic_auth(self):
        opener = OpenerDirector()
        ph = urllib2.ProxyHandler(dict(http="proxy.example.com:3128"))
        opener.add_handler(ph)
        password_manager = MockPasswordManager()
        auth_handler = urllib2.ProxyBasicAuthHandler(password_manager)
        realm = "ACME Networks"
        http_handler = MockHTTPHandler(
            407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
        opener.add_handler(auth_handler)
        opener.add_handler(http_handler)
        self._test_basic_auth(opener, auth_handler, "Proxy-authorization",
                              realm, http_handler, password_manager,
                              "http://acme.example.com:3128/protected",
                              "proxy.example.com:3128",
                              ) 
Example #5
Source File: test_urllib2.py    From gcblue with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_proxy_basic_auth(self):
        opener = OpenerDirector()
        ph = urllib2.ProxyHandler(dict(http="proxy.example.com:3128"))
        opener.add_handler(ph)
        password_manager = MockPasswordManager()
        auth_handler = urllib2.ProxyBasicAuthHandler(password_manager)
        realm = "ACME Networks"
        http_handler = MockHTTPHandler(
            407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
        opener.add_handler(auth_handler)
        opener.add_handler(http_handler)
        self._test_basic_auth(opener, auth_handler, "Proxy-authorization",
                              realm, http_handler, password_manager,
                              "http://acme.example.com:3128/protected",
                              "proxy.example.com:3128",
                              ) 
Example #6
Source File: test_urllib2.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def test_proxy_basic_auth(self):
        opener = OpenerDirector()
        ph = urllib2.ProxyHandler(dict(http="proxy.example.com:3128"))
        opener.add_handler(ph)
        password_manager = MockPasswordManager()
        auth_handler = urllib2.ProxyBasicAuthHandler(password_manager)
        realm = "ACME Networks"
        http_handler = MockHTTPHandler(
            407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
        opener.add_handler(auth_handler)
        opener.add_handler(http_handler)
        self._test_basic_auth(opener, auth_handler, "Proxy-authorization",
                              realm, http_handler, password_manager,
                              "http://acme.example.com:3128/protected",
                              "proxy.example.com:3128",
                              ) 
Example #7
Source File: test_urllib2.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def test_proxy_basic_auth(self):
        opener = OpenerDirector()
        ph = urllib2.ProxyHandler(dict(http="proxy.example.com:3128"))
        opener.add_handler(ph)
        password_manager = MockPasswordManager()
        auth_handler = urllib2.ProxyBasicAuthHandler(password_manager)
        realm = "ACME Networks"
        http_handler = MockHTTPHandler(
            407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
        opener.add_handler(auth_handler)
        opener.add_handler(http_handler)
        self._test_basic_auth(opener, auth_handler, "Proxy-authorization",
                              realm, http_handler, password_manager,
                              "http://acme.example.com:3128/protected",
                              "proxy.example.com:3128",
                              ) 
Example #8
Source File: test_urllib2.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def test_proxy_basic_auth(self):
        opener = OpenerDirector()
        ph = urllib2.ProxyHandler(dict(http="proxy.example.com:3128"))
        opener.add_handler(ph)
        password_manager = MockPasswordManager()
        auth_handler = urllib2.ProxyBasicAuthHandler(password_manager)
        realm = "ACME Networks"
        http_handler = MockHTTPHandler(
            407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
        opener.add_handler(auth_handler)
        opener.add_handler(http_handler)
        self._test_basic_auth(opener, auth_handler, "Proxy-authorization",
                              realm, http_handler, password_manager,
                              "http://acme.example.com:3128/protected",
                              "proxy.example.com:3128",
                              ) 
Example #9
Source File: errata_fetcher.py    From centos-package-cron with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_opener():
    default_opener = urllib2.build_opener()
    if not exists(YUM_CONF):
        return default_opener
    config = RawConfigParser()
    config.read(YUM_CONF)
    if not config.has_section('main'):
        return default_opener
    if not config.has_option('main', 'proxy'):
        return default_opener
    proxy = {}
    url = config.get('main', 'proxy').strip()
    if not url:
        return default_opener
    http_proxy_handler = urllib2.ProxyHandler({'http': url, 'https': url})
    # urllib2 can open HTTPS ressources through a proxy since python 2.6.3
    # should be OK on Centos OS (Python 2.6.6)
    if config.has_option('main', 'proxy_username') and config.has_option(
            'main', 'proxy_password'):
        username = config.get('main', 'proxy_username').strip()
        password = config.get('main', 'proxy_password').strip()
        password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
        password_manager.add_password(None, url, username, password)
        proxy_auth_handler = urllib2.ProxyBasicAuthHandler(password_manager)
        return urllib2.build_opener(http_proxy_handler, proxy_auth_handler)
    return urllib2.build_opener(http_proxy_handler) 
Example #10
Source File: lookaside.py    From conary with Apache License 2.0 4 votes vote down vote up
def _fetchUrl(self, url, headers):
        if isinstance(url, str):
            url = laUrl(url)

        retries = 3
        if self.cfg.proxy and not self.noproxyFilter.bypassProxy(url.host):
            retries = 7
        inFile = None
        for i in range(retries):
            try:
                # set up a handler that tracks cookies to handle
                # sites like Colabnet that want to set a session cookie
                cj = cookielib.LWPCookieJar()
                opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

                # add password handler if needed
                if url.user:
                    url.passwd = url.passwd or ''
                    opener.add_handler(
                            HTTPBasicAuthHandler(url.user, url.passwd))

                # add proxy and proxy password handler if needed
                if self.cfg.proxy and \
                        not self.noproxyFilter.bypassProxy(url.host):
                    proxyPasswdMgr = urllib2.HTTPPasswordMgr()
                    for v in self.cfg.proxy.values():
                        pUrl = laUrl(v[1])
                        if pUrl.user:
                            pUrl.passwd = pUrl.passwd or ''
                            proxyPasswdMgr.add_password(
                                None, pUrl.asStr(noAuth=True, quoted=True),
                                url.user, url.passwd)

                    opener.add_handler(
                        urllib2.ProxyBasicAuthHandler(proxyPasswdMgr))
                    opener.add_handler(
                        urllib2.ProxyHandler(self.cfg.proxy))

                if url.scheme == 'ftp':
                    urlStr = url.asStr(noAuth=False, quoted=True)
                else:
                    urlStr = url.asStr(noAuth=True, quoted=True)
                req = urllib2.Request(urlStr, headers=headers)

                inFile = opener.open(req)
                if not urlStr.startswith('ftp://'):
                    content_type = inFile.info().get('content-type')
                    if not url.explicit() and 'text/html' in content_type:
                        raise urllib2.URLError('"%s" not found' % urlStr)
                log.info('Downloading %s...', urlStr)
                break
            except urllib2.HTTPError, msg:
                if msg.code == 404:
                    return None
                else:
                    log.error('error downloading %s: %s',
                              urlStr, str(msg))
                    return None
            except urllib2.URLError:
                return None