Ejemplo n.º 1
0
    def query(self, query, ts_start, ts_end):
        # target = 'summarize({},"{}","avg")'.format(
        #    query, '99year') @TODO remove if not needed

        # build graphite url
        args = {
            '__auth_token': self.token,
            'target': query,
            'format': 'json',
            'from': str(ts_start),
            'until': str(ts_end),
        }
        url = '{}/render?'.format(self.url)
        for k, v in args.items():
            url += '{}={}&'.format(quote(k), quote(v))

        # Basic auth header
        password_mgr = HTTPPasswordMgrWithDefaultRealm()
        password_mgr.add_password(
            None,
            self.graphite_url,
            self.username,
            self.password,
        )
        auth_handler = HTTPBasicAuthHandler(password_mgr)
        opener = build_opener(auth_handler)
        install_opener(opener)

        result = json.loads(urlopen(url).read().decode('utf-8'))
        return result
Ejemplo n.º 2
0
    def foreach(self, server):
        """Helper function to iterate Graphite metrics using foreach_path

        We will return an empty element even though foreach_path is not set
        for convenience of the caller.
        """

        if self.foreach_path:
            formatter = AttributeFormatter()
            params = formatter.vformat('query=' + self.foreach_path, (),
                                       server)

            password_mgr = HTTPPasswordMgrWithDefaultRealm()
            password_mgr.add_password(
                None,
                settings.GRAPHITE_URL,
                settings.GRAPHITE_USER,
                settings.GRAPHITE_PASSWORD,
            )
            auth_handler = HTTPBasicAuthHandler(password_mgr)
            url = '{0}/metrics/find?{1}'.format(settings.GRAPHITE_URL, params)
            with build_opener(auth_handler).open(url) as response:
                return json.loads(response.read().decode())

        return [{
            'id': '',
            'leaf': 0,
            'context': {},
            'text': '',
            'expandable': 0,
            'allowChildren': 0,
        }]
    def download(self, source, dest):
        """
        Download an archive file.

        :param str source: URL pointing to an archive file.
        :param str dest: Local path location to download archive file to.
        """
        # propogate all exceptions
        # URLError, OSError, etc
        proto, netloc, path, params, query, fragment = urlparse(source)
        if proto in ('http', 'https'):
            auth, barehost = splituser(netloc)
            if auth is not None:
                source = urlunparse(
                    (proto, barehost, path, params, query, fragment))
                username, password = splitpasswd(auth)
                passman = HTTPPasswordMgrWithDefaultRealm()
                # Realm is set to None in add_password to force the username and password
                # to be used whatever the realm
                passman.add_password(None, source, username, password)
                authhandler = HTTPBasicAuthHandler(passman)
                opener = build_opener(authhandler)
                install_opener(opener)
        response = urlopen(source)
        try:
            with open(dest, 'wb') as dest_file:
                dest_file.write(response.read())
        except Exception as e:
            if os.path.isfile(dest):
                os.unlink(dest)
            raise e
Ejemplo n.º 4
0
 def _install_opener(self):
     self.opener = None
     if self.username != "":
         self.password_mgr = HTTPPasswordMgrWithDefaultRealm()
         self.password_mgr.add_password(None, self.baseurlauth,
                                        self.username, self.password)
         self.auth_handler = HTTPBasicAuthHandler(self.password_mgr)
     if self.ssl is True:
         if self.cafile == "":
             self.context = ssl.create_default_context()
             self.context.check_hostname = False
             self.context.verify_mode = ssl.CERT_NONE
         else:
             self.context = ssl.create_default_context()
             self.context.load_verify_locations(cafile=self.cafile)
             self.context.verify_mode = ssl.CERT_REQUIRED
         self.https_handler = HTTPSHandler(context=self.context)
         if self.username != "":
             self.opener = build_opener(self.https_handler,
                                        self.auth_handler)
         else:
             self.opener = build_opener(self.https_handler)
     else:
         if self.username != "":
             self.opener = build_opener(self.auth_handler)
     if self.opener is not None:
         self.log.debug("Setting up opener on: {}".format(self.baseurlauth))
         install_opener(self.opener)
Ejemplo n.º 5
0
  def execute(self, method, *args, **kwargs):
    header = {
        'Content-Type' : 'application/json',
        'User-Agent' : 'python-xbmc'
        }
    # Params are given as a dictionnary
    if len(args) == 1:
      args=args[0]
      params = kwargs
      # Use kwargs for param=value style
    else:
      args = kwargs
    params={}
    params['jsonrpc']='2.0'
    params['id']=self.id
    self.id +=1
    params['method']=method
    params['params']=args

    values=json.dumps(params)
    # HTTP Authentication
    password_mgr = HTTPPasswordMgrWithDefaultRealm()
    password_mgr.add_password(None, self.url, self.username, self.password)
    auth_handler = HTTPBasicAuthHandler(password_mgr)
    opener = build_opener(auth_handler)
    install_opener(opener)

    data = values
    req = Request(self.url, data.encode('utf-8'), header)
    response = urlopen(req)
    the_page = response.read()
    if len(the_page) > 0 :
      return json.load(StringIO(the_page.decode('utf-8')))
    else:
      return None # for readability
Ejemplo n.º 6
0
def unavco_dl(d, opt_dict):
    user_name = password_config.unavuser
    user_password = password_config.unavpass
    url = d['downloadUrl']
    passman = HTTPPasswordMgrWithDefaultRealm()
    passman.add_password(None, 'https://imaging.unavco.org/data/sar/lts',
                         user_name, user_password)
    authhandler = HTTPDigestAuthHandler(passman)
    opener = build_opener(authhandler)
    filename = os.path.basename(url)
    try:
        f = opener.open(url)
    except HTTPError as e:
        print(e)
        return
    dl_file_size = int(f.info()['Content-Length'])
    if os.path.exists(filename):
        file_size = os.path.getsize(filename)
        if dl_file_size == file_size:
            print("%s already downloaded" % filename)
            f.close()
            return
    start = time.time()
    CHUNK = 256 * 10240
    with open(filename, 'wb') as fp:
        while True:
            chunk = f.read(CHUNK)
            if not chunk: break
            fp.write(chunk)
    total_time = time.time() - start
    mb_sec = (os.path.getsize(filename) / (1024 * 1024.0)) / total_time
    print("%s download time: %.2f secs (%.2f MB/sec)" %
          (filename, total_time, mb_sec))
    f.close()
Ejemplo n.º 7
0
    def open(self,
             url='',
             user=None,
             password=None,
             auth=HTTPBasicAuthHandler):
        if self.connected:
            raise WSException('Already connected')

        try:
            passwords = HTTPPasswordMgrWithDefaultRealm()
            passwords.add_password(None, url, user, password)

            if user and password:
                self.opener = build_opener(WSRedirectHandler(),
                                           auth(passwords))
            else:
                self.opener = build_opener(WSRedirectHandler())

            self.opener.open(url)

            # remove this. (urllib2 ugly code !)
            #install_opener(self.opener)

            self.connected = True
            self.url = url
        except HTTPError as e:
            raise WSException('Connection error to {}'.format(url), e) from e
        except URLError as e:
            raise WSException('Connection error to {}'.format(url), e) from e

        return self
Ejemplo n.º 8
0
Archivo: util.py Proyecto: pyfisch/nltk
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)
Ejemplo n.º 9
0
def urlopen(url, headers=None, data=None, timeout=None):
    """
    An URL opener with the User-agent set to gPodder (with version)
    """
    username, password = username_password_from_url(url)
    if username is not None or password is not None:
        url = url_strip_authentication(url)
        password_mgr = HTTPPasswordMgrWithDefaultRealm()
        password_mgr.add_password(None, url, username, password)
        handler = HTTPBasicAuthHandler(password_mgr)
        opener = build_opener(handler)
    else:
        opener = build_opener()

    if headers is None:
        headers = {}
    else:
        headers = dict(headers)

    headers.update({'User-agent': USER_AGENT})
    request = Request(url, data=data, headers=headers)
    if timeout is None:
        return opener.open(request)
    else:
        return opener.open(request, timeout=timeout)
Ejemplo n.º 10
0
Archivo: http.py Proyecto: eht16/lstail
    def _setup_url_opener_if_necessary(self):
        if self._url_opener is not None:
            return

        kwargs = dict()

        # disable SSL verification if requested
        if not self._verify_ssl_certificates:
            ssl_hosts = [
                server for server in self._servers
                if server.url.startswith('https')
            ]
            if ssl_hosts:
                context = ssl.create_default_context()
                context.check_hostname = False
                context.verify_mode = ssl.CERT_NONE
                kwargs['context'] = context

        # setup URL openers - add pre-emptive basic authentication
        http_handler = HTTPHandler()
        https_handler = HTTPSHandler(**kwargs)
        password_manager = HTTPPasswordMgrWithDefaultRealm()
        auth_handlers = []
        # setup auth handler if we have any servers requiring authentication
        for server in self._servers:
            if server.username:
                password_manager.add_password(None, server.url,
                                              server.username, server.password)

        if password_manager.passwd:
            auth_handler = PreemptiveBasicAuthHandler(password_manager)
            auth_handlers.append(auth_handler)

        self._url_opener = build_opener(http_handler, https_handler,
                                        *auth_handlers)
Ejemplo n.º 11
0
def init_auth(user, password, url):
    """ Init authentication """
    pass_mgr = HTTPPasswordMgrWithDefaultRealm()
    pass_mgr.add_password(None, url, user, password)
    auth_handler = HTTPBasicAuthHandler(pass_mgr)
    opener = build_opener(auth_handler)
    install_opener(opener)
Ejemplo n.º 12
0
def basic_authentication_opener_factory(
    registry_url: str,
    username: str,
    password: str,
) -> OpenerDirector:
    """
    Return an opener director that authenticates requests using the Basic scheme.

    Args:
        registry_url (str): The URL of the container registry API.
        username (str): A username used for Basic authentication when retrieving an
            access token.
        password (str): A password used for Basic authentication when
            retrieving an access token.

    Returns:
        OpenerDirector: A director for making HTTP requests. It's main method is `open`.

    """
    opener = opener_factory()
    password_manager = HTTPPasswordMgrWithDefaultRealm()
    password_manager.add_password(
        None,  # type: ignore
        registry_url,
        username,
        password,
    )
    opener.add_handler(HTTPBasicAuthHandler(password_manager))
    return opener
Ejemplo n.º 13
0
Archivo: fetch.py Proyecto: jayvdb/osc
    def __init__(self,
                 cachedir='/tmp',
                 api_host_options={},
                 urllist=[],
                 http_debug=False,
                 cookiejar=None,
                 offline=False,
                 enable_cpio=True):
        # set up progress bar callback
        self.progress_obj = None
        if sys.stdout.isatty():
            self.progress_obj = create_text_meter(use_pb_fallback=False)

        self.cachedir = cachedir
        self.urllist = urllist
        self.http_debug = http_debug
        self.offline = offline
        self.cpio = {}
        self.enable_cpio = enable_cpio

        passmgr = HTTPPasswordMgrWithDefaultRealm()
        for host in api_host_options:
            passmgr.add_password(None, host, api_host_options[host]['user'],
                                 api_host_options[host]['pass'])
        openers = (HTTPBasicAuthHandler(passmgr), )
        if cookiejar:
            openers += (HTTPCookieProcessor(cookiejar), )
        self.gr = OscFileGrabber(progress_obj=self.progress_obj)
Ejemplo n.º 14
0
    def authenticate(self, username, password):
        manager = HTTPPasswordMgrWithDefaultRealm()
        manager.add_password(None, BASE_URL_AUTHENTICATED, username, password)
        handler = HTTPBasicAuthHandler(manager)

        self.baseurl = BASE_URL_AUTHENTICATED
        self.opener.add_handler(handler)
Ejemplo n.º 15
0
def graph(request):
    """Proxy Graphite graphs

    We don't want to bother the user with authenticating to Graphite.
    Instead, here we download the graph using our credentials and pass
    it to the user.
    """
    password_mgr = HTTPPasswordMgrWithDefaultRealm()
    password_mgr.add_password(
        None,
        settings.GRAPHITE_URL,
        settings.GRAPHITE_USER,
        settings.GRAPHITE_PASSWORD,
    )
    auth_handler = HTTPBasicAuthHandler(password_mgr)
    url = '{0}/render?{1}'.format(settings.GRAPHITE_URL,
                                  request.GET.urlencode())

    # If the Graphite server fails, we would return proper server error
    # to the user instead of failing.  This is not really a matter for
    # the user as they would get a 500 in any case, but it is a matter for
    # the server.  We expect any kind of IO error in here, but the socket
    # errors are more likely to happen.  Graphite has the tendency to return
    # empty result with 200 instead of proper error codes.
    try:
        with build_opener(auth_handler).open(url) as response:
            return HttpResponse(response.read(), content_type='image/png')
    except IOError as error:
        return HttpResponseServerError(str(error))
Ejemplo n.º 16
0
 def __init__(self,
              server,
              directory="",
              username=None,
              password=None,
              proxy="",
              timeout=10,
              apipath=None,
              useHTTPS=True,
              source=__name__):
     """
     parameters
     *  server (string)        name of the server the account is located on
     *  directory (string)     if the friendica instance is located in a
                               subdirectory, specify it here
     *  apipath (string)       alternatively to calculate the API path from
                               server name and installation directory you
                               can specify the path here
     *  username (string)      account name => username@servername
     *  password (string)      the password for the account
     *  proxy (string)         this proxy will be used for connections
                               to the server
     *  timeout (integer)      seconds to wait for the response during
                               network requests, default is 10 seconds
     *  useHTTPS (boolean)     use HTTPS (true) or not (false) default is
                               to use HTTPS and will fallback to HTTP if
                               that does not work
     *  source (string)        this string will be used as source string,
                               e.g. client name, when publishing things
     """
     self.server = server
     self.directory = directory
     if (apipath == None):
         self.apipath = self.server + '/' + directory
         if len(directory):
             if not (directory[-1] == '/'):
                 self.apipath = self.apipath + '/'
         self.apipath = self.apipath + 'api'
     else:
         self.apipath = apipath
     self.username = username
     self.password = password
     self.proxy = proxy
     self.timeout = timeout
     self.useHTTPS = useHTTPS
     self.source = source
     self.cj = CookieJar()
     self.pwd_mgr = HTTPPasswordMgrWithDefaultRealm()
     self.pwd_mgr.add_password(None,
                               self.protocol() + self.apipath,
                               self.username, self.password)
     self.handler = HTTPBasicAuthHandler(self.pwd_mgr)
     if not self.proxy:
         self.opener = build_opener(self.handler,
                                    HTTPCookieProcessor(self.cj))
     else:
         self.proxy_handler = ProxyHandler({'http': proxy, 'https': proxy})
         self.opener = build_opener(self.proxy_handler, self.handler,
                                    HTTPCookieProcessor(self.cj))
Ejemplo n.º 17
0
 def __init__(self, url, user=None, password=None):
     self.user = user
     self.password = password
     self.url = url
     password_manager = HTTPPasswordMgrWithDefaultRealm()
     password_manager.add_password(None, url, user, password)
     auth_manager = HTTPBasicAuthHandler(password_manager)
     self.opener = build_opener(auth_manager)
Ejemplo n.º 18
0
 def set_authentication(self, uri, login, password):
     password_manager = HTTPPasswordMgrWithDefaultRealm()
     password_manager.add_password(realm=None,
                                   uri=uri,
                                   user=login,
                                   passwd=password)
     self.http_opener = build_opener(
         HTTPBasicAuthHandler(password_manager),
         HTTPDigestAuthHandler(password_manager))
Ejemplo n.º 19
0
def create_opener(aur_server_tag: str) -> OpenerDirector:
    server = _aur_server(aur_server_tag)
    password_manager = HTTPPasswordMgrWithDefaultRealm()
    password_manager.add_password(realm=None,
                                  uri=server.address,
                                  user=server.user,
                                  passwd=server.password)
    handler = HTTPBasicAuthHandler(password_manager)
    return build_opener(handler)
Ejemplo n.º 20
0
def get_web_page(url, username=None, password=None, login_url=None):
    """Get url page possible with username and password.
    """

    if login_url:
        # Login via a form
        cookies = HTTPCookieProcessor()
        opener = build_opener(cookies)
        install_opener(opener)

        opener.open(login_url)

        try:
            token = [
                x.value for x in cookies.cookiejar if x.name == 'csrftoken'
            ][0]
        except IndexError:
            return False, "no csrftoken"

        params = dict(
            username=username,
            password=password,
            this_is_the_login_form=True,
            csrfmiddlewaretoken=token,
        )
        encoded_params = urlencode(params)

        with contextlib.closing(opener.open(login_url, encoded_params)) as f:
            f.read()
    elif username is not None:
        # Login using basic auth

        # Create password manager
        passman = HTTPPasswordMgrWithDefaultRealm()
        passman.add_password(None, url, username, password)

        # create the handler
        authhandler = HTTPBasicAuthHandler(passman)
        opener = build_opener(authhandler)
        install_opener(opener)

    try:
        pagehandle = urlopen(url)
    except HTTPError as e:
        msg = ('The server couldn\'t fulfill the request. '
               'Error code: %s' % e.status_code)
        e.args = (msg, )
        raise
    except URLError as e:
        msg = f'Could not open URL "{url}": {e}'
        e.args = (msg, )
        raise
    else:
        page = pagehandle.read()

    return page
Ejemplo n.º 21
0
    def __init__(self,
                 *,
                 username='******',
                 password='******',
                 top_level_url="https://urs.earthdata.nasa.gov/"):

        auth_manager = HTTPPasswordMgrWithDefaultRealm()
        auth_manager.add_password(None, top_level_url, username, password)
        handler = HTTPBasicAuthHandler(auth_manager)
        self.opener = urllib.request.build_opener(
            handler, HTTPCookieProcessor(CookieJar()))
Ejemplo n.º 22
0
 def open(request):
     request = request_vim_to_python(request)
     rhandler = HTTPRedirectHandler()
     rhandler.max_redirections = request['max_redirect']
     opener = build_opener(rhandler)
     if request['username']:
         passmgr = HTTPPasswordMgrWithDefaultRealm()
         passmgr.add_password(
             None,
             request['url'],
             request['username'],
             request['password'],
         )
         opener.add_handler(HTTPBasicAuthHandler(passmgr))
         opener.add_handler(HTTPDigestAuthHandler(passmgr))
     req = Request(
         url=request['url'],
         data=request['data'],
         headers=request['headers'],
         method=request['method'],
     )
     if request['gzip_decompress']:
         req.add_header('Accept-encoding', 'gzip')
     try:
         res = retry(tries=request['retry'])(opener.open)(
             req, timeout=request['timeout'])
     except HTTPError as e:
         res = e
     if not hasattr(res, 'version'):
         # urllib2 does not have 'version' field
         import httplib
         res.version = httplib.HTTPConnection._http_vsn
     response_status = "HTTP/%s %d %s\n" % (
         '1.1' if res.version == 11 else '1.0',
         res.code,
         res.msg,
     )
     response_headers = str(res.headers)
     response_body = res.read()
     if (request['gzip_decompress']
             and res.headers.get('Content-Encoding') == 'gzip'):
         response_body = gzip_decompress(response_body)
     if hasattr(res.headers, 'get_content_charset'):
         # Python 3
         response_encoding = res.headers.get_content_charset()
     else:
         # Python 2
         response_encoding = res.headers.getparam('charset')
     response_body = response_body.decode(response_encoding)
     return (
         request['url'],
         response_status + response_headers,
         response_body,
     )
Ejemplo n.º 23
0
def authorize():
    mal_config = read_mal_config()

    pass_manager = HTTPPasswordMgrWithDefaultRealm()
    pass_manager.add_password(None, 'http://myanimelist.net/api',
                              mal_config['UserName'], mal_config['Password'])

    auth_handler = HTTPBasicAuthHandler(pass_manager)

    opener = build_opener(auth_handler)
    install_opener(opener)
Ejemplo n.º 24
0
def index(username, password):
    global url
    p = HTTPPasswordMgrWithDefaultRealm()
    p.add_password(None, url, username, password)
    auth_handler = HTTPBasicAuthHandler(p)
    opener = build_opener(auth_handler)
    try:
        result = opener.open(url)
        html = result.read().decode('utf-8')
        print(html)
    except URLError as e:
        print(e.reason)
Ejemplo n.º 25
0
def url_get(url, user, password):
    top_level_url = url.rsplit('/', 1)

    password_mgr = HTTPPasswordMgrWithDefaultRealm()
    password_mgr.add_password(None, top_level_url, user, password)

    handler = HTTPBasicAuthHandler(password_mgr)

    opener = build_opener(handler)
    opener.open(url)
    install_opener(opener)

    return urlopen(url)
Ejemplo n.º 26
0
 def __init__(self, username, password=None):
     """Class to store info required to connect to the web server"""
     # Get password if necessary
     if password is None:
         password = getpass()
     # Get URL for the database
     self.db_url = "http://virgodb.dur.ac.uk:8080/Eagle/"
     # Set up authentication and cookies
     self.password_mgr = HTTPPasswordMgrWithDefaultRealm()
     self.password_mgr.add_password(None, self.db_url, username, password)
     self.opener = OpenerDirector()
     self.auth_handler = HTTPBasicAuthHandler(self.password_mgr)
     self.cookie_handler = HTTPCookieProcessor(cookie_jar)
Ejemplo n.º 27
0
def authenticated_urlopen(location):
    """ A wrapper around urlopen adding authentication information if provided by the user. """
    passman = HTTPPasswordMgrWithDefaultRealm()
    server_name = urlparse.urlsplit(location).netloc
    access = get_server_access(server_name)
    if access is not None:
        user = access.username
        password = access.password
        if user is not None and password is not None:
            passman.add_password(None, location, user, password)
    authhandler = HTTPBasicAuthHandler(passman)
    opener = build_opener(authhandler)
    install_opener(opener)
    return urlopen(location)
Ejemplo n.º 28
0
def get_port_info_from_url(port):
    url = 'http://192.168.38.128:8181/restconf/operational/opendaylight-inventory:nodes/node/' + str(
        port.node) + '/node-connector/' + str(port) + '/'
    username = '******'
    password = '******'
    p = HTTPPasswordMgrWithDefaultRealm()

    p.add_password(None, url, username, password)

    handler = HTTPBasicAuthHandler(p)
    opener = build_opener(handler)
    install_opener(opener)

    content = urlopen(url).read()
    #print(content)

    data = content.decode("utf-8")
    print(data)

    filename = 'port_info.json'
    with open(filename, 'w') as outfile:
        json.dump(data, outfile)

    with fileinput.FileInput(filename, inplace=True) as file:
        for line in file:
            print(line.replace("\\", "").replace("\"{",
                                                 "{").replace("}\"", "}"),
                  end='')

    client = MongoClient('localhost', 27017)
    db = client.nets
    db.port_info.remove({})
    mongoimport('localhost', 27017, 'nets', 'port_info', 'port_info.json')

    datos = {}

    link_down = query_get_link_down_port_info()
    datos["link-down"] = link_down
    blocked = query_get_blocked_port_info()
    datos["blocked"] = blocked
    tbytes = query_get_bytes_transmitted_port_info()
    datos["tbytes"] = tbytes
    rbytes = query_get_bytes_received_port_info()
    datos["rbytes"] = rbytes
    drops = query_get_receive_drops_port_info()
    datos["drops"] = drops

    print(datos)
    return datos
Ejemplo n.º 29
0
    def __init__(self, device_data):
        """Set up how we are going to connect to the ICS Calendar"""
        self.name = device_data[CONF_NAME]
        self.url = device_data[CONF_URL]
        self.include_all_day = device_data[CONF_INCLUDE_ALL_DAY]
        self.event = None

        if device_data[CONF_USERNAME] != '' \
           and device_data[CONF_PASSWORD] != '':
           passman = HTTPPasswordMgrWithDefaultRealm()
           passman.add_password(None, self.url, device_data[CONF_USERNAME], device_data[CONF_PASSWORD])
           basicAuthHandler = HTTPBasicAuthHandler(passman)
           digestAuthHandler = HTTPDigestAuthHandler(passman)
           opener = build_opener(digestAuthHandler, basicAuthHandler)
           install_opener(opener)
Ejemplo n.º 30
0
def init_auth(user, password, url, use_ssl=False):
    """ Init authentication """
    pass_mgr = HTTPPasswordMgrWithDefaultRealm()
    pass_mgr.add_password(None, url, user, password)
    auth_handler = HTTPBasicAuthHandler(pass_mgr)

    if use_ssl:
        import ssl
        from urllib.request import HTTPSHandler

        opener = build_opener(auth_handler, HTTPSHandler(context=ssl._create_unverified_context()))
    else:
        opener = build_opener(auth_handler)

    install_opener(opener)