Currently not working...
    # https://docs.python.org/3/library/urllib.request.html#examples
    # Create an OpenerDirector with support for Basic HTTP Authentication...
347120
def download_file(p_realm, p_url, p_op_file, p_username, p_password):
    
    auth_handler = urllib.request.HTTPBasicAuthHandler()
    auth_handler.add_password(realm=p_realm,
                              uri=p_url,
                              user=p_username,
                              passwd=p_password)
    opener = urllib.request.build_opener(auth_handler)
    # ...and install it globally so it can be used with urlopen.
    urllib.request.install_opener(opener)

    web = urllib.request.urlopen(p_url)
    with open(p_op_file, 'w') as f:
        f.write(web.read().decode('utf-8'))
443893
def get(url, **kwargs):
    
    headers = kwargs.get('headers', {})
    headers['User-Agent'] = config.USER_AGENT # overwrite
    kwargs['headers'] = headers

    timeout = kwargs.get('timeout', config.TIMEOUT)
    kwargs['timeout'] = timeout

    kwargs['verify'] = False # no SSLError

    logger.debug("Getting: %s", url)
    return requests.get(url, **kwargs)
1062991
def github_request(self, path, callback, access_token=None,
                       post_args=None, **kwargs):
        
        url = self._API_URL + path
        all_args = {}
        if access_token:
            all_args["access_token"] = access_token
            all_args.update(kwargs)
        if all_args:
            url += "?" + auth.urllib_parse.urlencode(all_args)
        callback = self.async_callback(self._on_github_request, callback)
        http = self._get_auth_http_client()
        if post_args is not None:
            http.fetch(url, method="POST",
                       user_agent='Tinman/Tornado',
                       body=auth.urllib_parse.urlencode(post_args),
                       callback=callback)
        else:
            http.fetch(url, user_agent='Tinman/Tornado', callback=callback)
540182
def from_url(cls, url: URL,
                 *, encoding: str='latin1') -> Optional['BasicAuth']:
        """"""
        if not isinstance(url, URL):
            raise TypeError("url should be yarl.URL instance")
        if url.user is None:
            return None
        return cls(url.user, url.password or '', encoding=encoding)
248638
def request(self, url, params={}, headers={}):
        
        # Generate current config. Merge in any given headers
        cur_config = self.config.copy()
        if 'headers' in cur_config:
            cur_config['headers'] = cur_config['headers'].copy()
            cur_config['headers'].update(headers)
        else:
            cur_config['headers'] = headers

        with closing(requests.get(url, params=params, **cur_config)) as response:
            if response.status_code == requests.codes.OK:
                # Prepare the temp file. xml content will be
                # stored in a binary file, json in a textfile.
                if (response.headers.get('Content-Type')
                        and ('json' in response.headers['Content-Type'])):
                    enc, fmode = response.encoding, 'w+t'
                else:
                    enc, fmode = None, 'w+b'
                # Create temp file ensuring 2to3 compatibility
                if str_type == str:  # we are on py3
                    source = STF(
                        max_size=self.max_size, mode=fmode, encoding=enc)
                else:
                    # On py27 we must omit the 'encoding' kwarg
                    source = STF(max_size=self.max_size, mode=fmode)
                for c in response.iter_content(chunk_size=1000000,
                                               decode_unicode=bool(enc)):
                    source.write(c)

            else:
                source = None
            code = int(response.status_code)
            if 400 <= code <= 499:
                raise response.raise_for_status()
            return source, response.url, response.headers, code
401706
def request(self, req):
        
        data = json.dumps(req)
        req = urllib2.Request(self.url, data, self.headers)
        f = self.opener.open(req)
        resp = f.read()
        f.close()
        return json.loads(resp)
1272004
def _urllib_send(opener, req, **kwargs):
    """"""
    if req.content and not any(h.lower() == 'content-type'
                               for h in req.headers):
        req = req.with_headers({'Content-Type': 'application/octet-stream'})
    url = req.url + '?' + urlencode(req.params)
    raw_req = urllib_request.Request(url, req.content, headers=req.headers)
    set_urllib_method(raw_req, req.method)
    try:
        res = opener.open(raw_req, **kwargs)
    except urllib_http_error_cls as http_err:
        res = http_err
    return Response(res.getcode(), content=res.read(), headers=res.headers)
709855
def request(self, request_path, data=None, do_authentication=True, is_json=True):
        
        uri = self.api_uri % request_path
        request = urllib2.Request(uri)

        # Build up the request
        if is_json:
            request.add_header("Content-Type", "application/json")
            if data is not None:
                request.add_data(json.dumps(data))
        elif data is not None:
            request.add_data(data)

        if do_authentication:
            if self.client_id is None or self.client_secret is None:
                raise Exception(u"You need to supply a client_id and client_secret to perform an authenticated request")
            basic_auth = base64.b64encode("%s:%s" % (self.client_id, self.client_secret))
            request.add_header("Authorization", "Basic %s" % basic_auth)

        try:
            response = self._make_request(request)
        except Exception as inst:
            raise # automatically re-raises the exception

        if 'status' in response:
            # Grab the status info if it exists
            self._last_status_code = response['status']['code']
            if 'message' in response['status']:
                self._last_status_message = response['status']['message']

            if 'data' in response:
                return response['data']

        return response
864234
def _request(self, url, method = u"get", data = None, headers=None, **kwargs):
        
        #        if self.access_token:
        #            auth_header = {
        #                u"Authorization": "Bearer %s" % (self.access_token)
        #            }
        #            headers.update(auth_header)
        #basic auth
        msg = "method: %s url:%s\nheaders:%s\ndata:%s" % (
            method, url, headers, data)
        #print msg
        if not self.use_oauth:
            auth = (self.sk_user, self.sk_pw)
            if not self.client:
                self.client = requests.session()
            r = self.client.request(method, url, headers=headers, data=data, auth=auth,**kwargs)
        else:
            if not self.client:
                self.client = requests.session(hooks={'pre_request': oauth_hook})
            r = self.client.request(method, url, headers=headers, data=data,**kwargs)
        return r
643794
def setup(self, proxystr='', prompting=True):
        
        self.prompting = prompting
        proxy = self.get_proxy(proxystr)
        if proxy:
            proxy_support = urllib2.ProxyHandler({"http": proxy, "ftp": proxy})
            opener = urllib2.build_opener(proxy_support, urllib2.CacheFTPHandler)
            urllib2.install_opener(opener)
388257
def open(url):
    
    if sys.platform == 'win32':
        os.startfile(url)
    elif sys.platform == 'darwin':
        subprocess.Popen(['open', url])
    else:
        try:
            subprocess.Popen(['xdg-open', url])
        except OSError:
            import webbrowser
            webbrowser.open(url)
72854
def getCert(certHost=vos.vos.SERVER, certfile=None,
            certQuery="/cred/proxyCert?daysValid=",daysValid=2):
    """"""

    if certfile is None:
        certfile = os.path.join(os.getenv("HOME","/tmp"),".ssl/cadcproxy.pem")

    dirname = os.path.dirname(certfile)
    try:
        os.makedirs(dirname)
    except OSError as e:
        if os.path.isdir(dirname):
            pass
        elif e.errno == 20 or e.errno == 17:
            sys.stderr.write(str(e)+": %s \n" % dirname)
            sys.stderr.write("Expected %s to be a directory.\n" % ( dirname))
            sys.exit(e.errno)
        else:
            raise e
    
    # Example taken from voidspace.org.uk
    # create a password manager
    password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()

    (username, passwd) = getUserPassword(host=certHost)

    # Add the username and password.
    # If we knew the realm, we could use it instead of ``None``.
    top_level_url = "http://"+certHost
    password_mgr.add_password(None, top_level_url, username, passwd)
    
    handler = urllib2.HTTPBasicAuthHandler(password_mgr)
    
    # create "opener" (OpenerDirector instance)
    opener = urllib2.build_opener(handler)
    
    # Install the opener.   
    urllib2.install_opener(opener)

    # Now all calls to urllib2.urlopen use our opener.
    url="http://"+certHost+certQuery+str(daysValid)
    r= urllib2.urlopen(url)
    w= file(certfile,'w')
    while True:
        buf=r.read()
        if not buf:
            break
        w.write(buf)
    w.close()
    r.close()
    return
1094307
def request(self, method, url, params=None, data=None, headers=None, auth=None,
                timeout=None, allow_redirects=False):
        
        raise TwilioException('HttpClient is an abstract class')
562700
def get_response(self, url, username=None, password=None):
        
        scheme, netloc, path, query, frag = urlparse.urlsplit(url)
        req = self.get_request(url)

        stored_username, stored_password = self.passman.find_user_password(None, netloc)
        # see if we have a password stored
        if stored_username is None:
            if username is None and self.prompting:
                username = urllib.quote(raw_input('User for %s: ' % netloc))
                password = urllib.quote(getpass.getpass('Password: '))
            if username and password:
                self.passman.add_password(None, netloc, username, password)
            stored_username, stored_password = self.passman.find_user_password(None, netloc)
        authhandler = urllib2.HTTPBasicAuthHandler(self.passman)
        opener = urllib2.build_opener(authhandler)
        # FIXME: should catch a 401 and offer to let the user reenter credentials
        return opener.open(req)
640478
def _auth(url):
    '''
    
    '''
    user = __salt__['config.get']('solr.user', False)
    password = __salt__['config.get']('solr.passwd', False)
    realm = __salt__['config.get']('solr.auth_realm', 'Solr')

    if user and password:
        basic = _HTTPBasicAuthHandler()
        basic.add_password(
            realm=realm, uri=url, user=user, passwd=password
        )
        digest = _HTTPDigestAuthHandler()
        digest.add_password(
            realm=realm, uri=url, user=user, passwd=password
        )
        _install_opener(
            _build_opener(basic, digest)
        )
513710
def doQuery(self, url, method='GET', getParmeters=None, postParameters=None, files=None, extraHeaders={}, session={}):
        """"""

        # Build headers
        headers = {}

        if not postParameters:
            postParameters = {}

        for key, value in extraHeaders.iteritems():
            # Fixes #197 for values with utf-8 chars to be passed into plugit
            if isinstance(value, basestring):
                headers['X-Plugit-' + key] = value.encode('utf-8')
            else:
                headers['X-Plugit-' + key] = value

        for key, value in session.iteritems():
            headers['X-Plugitsession-' + key] = value
            if 'Cookie' not in headers:
                headers['Cookie'] = ''
            headers['Cookie'] += key + '=' + str(value) + '; '

        if method == 'POST':
            if not files:
                r = requests.post(self.baseURI + '/' + url, params=getParmeters, data=postParameters, stream=True, headers=headers)
            else:
                # Special way, for big files
                # Requests is not usable: https://github.com/shazow/urllib3/issues/51

                from poster.encode import multipart_encode, MultipartParam
                from poster.streaminghttp import register_openers
                import urllib2
                import urllib

                # Register the streaming http handlers with urllib2
                register_openers()

                # headers contains the necessary Content-Type and Content-Length
                # datagen is a generator object that yields the encoded parameters
                data = []
                for x in postParameters:
                    if isinstance(postParameters[x], list):
                        for elem in postParameters[x]:
                            data.append((x, elem))
                    else:
                        data.append((x, postParameters[x]))

                for f in files:
                    data.append((f, MultipartParam(f, fileobj=open(files[f].temporary_file_path(), 'rb'), filename=files[f].name)))

                datagen, headers_multi = multipart_encode(data)

                headers.update(headers_multi)

                if getParmeters:
                    get_uri = '?' + urllib.urlencode(getParmeters)
                else:
                    get_uri = ''

                # Create the Request object
                request = urllib2.Request(self.baseURI + '/' + url + get_uri, datagen, headers)

                re = urllib2.urlopen(request)

                from requests import Response

                r = Response()
                r.status_code = re.getcode()
                r.headers = dict(re.info())
                r.encoding = "application/json"
                r.raw = re.read()
                r._content = r.raw

                return r

        else:
            # Call the function based on the method.
            r = requests.request(method.upper(), self.baseURI + '/' + url, params=getParmeters, stream=True, headers=headers, allow_redirects=True)

        return r
1022289
def open_url(self, url):
        
        try:
            c = pycurl.Curl()
            c.setopt(pycurl.FAILONERROR, True)
            c.setopt(pycurl.URL, "%s/api/v0/%s" % (self.url, url))
            c.setopt(pycurl.HTTPHEADER, ["User-Agent: %s" % self.userAgent,
                                         "apiToken: %s" % self.apiToken])
            b = StringIO.StringIO()
            c.setopt(pycurl.WRITEFUNCTION, b.write)
            c.setopt(pycurl.FOLLOWLOCATION, 1)
            c.setopt(pycurl.MAXREDIRS, 5)
            # Persoonlijkrooster only supports SSLv3, not the default SSLv23.
            c.setopt(pycurl.SSLVERSION, pycurl.SSLVERSION_SSLv3) 
            # Verify authenticity of peer's certificate. (0 will not check it)
            c.setopt(pycurl.SSL_VERIFYPEER, 1)
            # Verify that domain in URL matches to the Common Name Field or
            # a Subject Alternate Name field in the certificate.
            # (0 will not check it; 1 is an invalid value)
            c.setopt(pycurl.SSL_VERIFYHOST, 2) 
            c.perform()
            return b.getvalue()
        except pycurl.error, e:
            raise MyTimetableError(e)