示例#1
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)
    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
示例#3
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
示例#4
0
文件: util.py 项目: 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)
示例#5
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)
    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)
示例#7
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, 'w') as dest_file:
                dest_file.write(response.read())
        except Exception as e:
            if os.path.isfile(dest):
                os.unlink(dest)
            raise e
示例#8
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()
示例#9
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))
示例#10
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
示例#11
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)
示例#12
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
示例#13
0
文件: fetch.py 项目: 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)
示例#14
0
文件: fetch.py 项目: JLahti/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
        if sys.stdout.isatty() and TextMeter:
            self.progress_obj = TextMeter(fo=sys.stdout)
        else:
            self.progress_obj = None

        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)
示例#15
0
文件: http.py 项目: 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)
示例#16
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
示例#17
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,
        }]
示例#18
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
示例#19
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))
 def __init__(self, host, port, username, password):
     self.url = "http://%s:%s" % (host, port)
     
     pwdmgr = HTTPPasswordMgrWithDefaultRealm()
     pwdmgr.add_password(None, self.url, username, password)
     pwdhandler = HTTPBasicAuthHandler(pwdmgr)
     
     self.opener = build_opener(pwdhandler)
示例#21
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)
示例#22
0
def openURL(url_base, data, method='Get', cookies=None, username=None, password=None):
    ''' function to open urls - wrapper around urllib2.urlopen but with additional checks for OGC service exceptions and url formatting, also handles cookies and simple user password authentication'''
    url_base.strip() 
    lastchar = url_base[-1]
    if lastchar not in ['?', '&']:
        if url_base.find('?') == -1:
            url_base = url_base + '?'
        else:
            url_base = url_base + '&'
            
    if username and password:
        # Provide login information in order to use the WMS server
        # Create an OpenerDirector with support for Basic HTTP 
        # Authentication...
        passman = HTTPPasswordMgrWithDefaultRealm()
        passman.add_password(None, url_base, username, password)
        auth_handler = HTTPBasicAuthHandler(passman)
        opener = urllib.request.build_opener(auth_handler)
        openit = opener.open
    else:
        # NOTE: optionally set debuglevel>0 to debug HTTP connection
        #opener = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=0))
        #openit = opener.open
        openit = urlopen
   
    try:
        if method == 'Post':
            req = Request(url_base, data)
            # set appropriate header if posting XML
            try:
                xml = etree.fromstring(data)
                req.add_header('Content-Type', "text/xml")
            except:
                pass
        else:
            req=Request(url_base + data)
        if cookies is not None:
            req.add_header('Cookie', cookies)
        u = openit(req)
    except HTTPError as e: #Some servers may set the http header to 400 if returning an OGC service exception or 401 if unauthorised.
        if e.code in [400, 401]:
            raise ServiceException(e.read())
        else:
            raise e
    # check for service exceptions without the http header set
    if u.info()['Content-Type'] in ['text/xml', 'application/xml']:          
        #just in case 400 headers were not set, going to have to read the xml to see if it's an exception report.
        #wrap the url stram in a extended StringIO object so it's re-readable
        u=RereadableURL(u)      
        se_xml= u.read()
        se_tree = etree.fromstring(se_xml)
        serviceException=se_tree.find('{http://www.opengis.net/ows}Exception')
        if serviceException is None:
            serviceException=se_tree.find('ServiceException')
        if serviceException is not None:
            raise ServiceException(str(serviceException.text).strip())
        u.seek(0) #return cursor to start of u      
    return u
示例#23
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))
示例#24
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)
示例#25
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)
示例#26
0
 def init_opener(self):
     """ chinachuのAPIを実行するためのopenerを初期化する """
     pm = HTTPPasswordMgrWithDefaultRealm()
     url = self._config["chinachu"]["apiEndpoint"]
     user = self._config["chinachu"]["username"]
     password = self._config["chinachu"]["password"]
     pm.add_password(None, url, user, password)
     handler = HTTPBasicAuthHandler(pm)
     self._opener = build_opener(handler)
示例#27
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
示例#28
0
class AuthorizationHandler:
    def __init__(self):
        self.password_mgr = HTTPPasswordMgrWithDefaultRealm()

    def basic_auth(self):
        handler = HTTPBasicAuthHandler(self.password_mgr)
        return handler

    def add_auth_data(self, top_level_url, user, passwd):
        self.password_mgr.add_password(None, top_level_url, user, passwd)
示例#29
0
    def auth(cls, username, password, uri, realm=None, timeout=None):
        '''Create an httplib1 instance witn a basic authentication handler.
The authentication'''
        if realm is None:
            password_mgr = HTTPPasswordMgrWithDefaultRealm()
        else:
            password_mgr = HTTPPasswordMgr()
        password_mgr.add_password(realm, uri, user, passwd)
        opener = HTTPBasicAuthHandler(password_mgr)
        return cls(opener,timeout)
示例#30
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)
示例#31
0
文件: mal.py 项目: Eskat0n/MALnet_bot
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)
示例#32
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()))
示例#33
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,
     )
示例#34
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)
示例#35
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)
示例#36
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)
示例#37
0
文件: https.py 项目: ByReaL/suds-test
class HttpAuthenticated(HttpTransport):
    """
    Provides basic http authentication that follows the RFC-2617 specification.
    As defined by specifications, credentials are provided to the server
    upon request (HTTP/1.0 401 Authorization Required) by the server only.
    @ivar pm: The password manager.
    @ivar handler: The authentication handler.
    """

    def __init__(self, **kwargs):
        """
        @param kwargs: Keyword arguments.
            - B{proxy} - An http proxy to be specified on requests.
                 The proxy is defined as {protocol:proxy,}
                    - type: I{dict}
                    - default: {}
            - B{timeout} - Set the url open timeout (seconds).
                    - type: I{float}
                    - default: 90
            - B{username} - The username used for http authentication.
                    - type: I{str}
                    - default: None
            - B{password} - The password used for http authentication.
                    - type: I{str}
                    - default: None
        """
        HttpTransport.__init__(self, **kwargs)
        self.pm = HTTPPasswordMgrWithDefaultRealm()

    def open(self, request):
        self.addcredentials(request)
        return HttpTransport.open(self, request)

    def send(self, request):
        self.addcredentials(request)
        return HttpTransport.send(self, request)

    def addcredentials(self, request):
        credentials = self.credentials()
        if not (None in credentials):
            u = credentials[0]
            p = credentials[1]
            self.pm.add_password(None, request.url, u, p)

    def credentials(self):
        return (self.options.username, self.options.password)

    def u2handlers(self):
            handlers = HttpTransport.u2handlers(self)
            handlers.append(HTTPBasicAuthHandler(self.pm))
            return handlers
示例#38
0
class HttpAuthenticated(HttpTransport):
    """
    Provides basic http authentication that follows the RFC-2617 specification.
    As defined by specifications, credentials are provided to the server
    upon request (HTTP/1.0 401 Authorization Required) by the server only.
    @ivar pm: The password manager.
    @ivar handler: The authentication handler.
    """
    
    def __init__(self, **kwargs):
        """
        @param kwargs: Keyword arguments.
            - B{proxy} - An http proxy to be specified on requests.
                 The proxy is defined as {protocol:proxy,}
                    - type: I{dict}
                    - default: {}
            - B{timeout} - Set the url open timeout (seconds).
                    - type: I{float}
                    - default: 90
            - B{username} - The username used for http authentication.
                    - type: I{str}
                    - default: None
            - B{password} - The password used for http authentication.
                    - type: I{str}
                    - default: None
        """
        HttpTransport.__init__(self, **kwargs)
        self.pm = HTTPPasswordMgrWithDefaultRealm()
        
    def open(self, request):
        self.addcredentials(request)
        return  HttpTransport.open(self, request)
    
    def send(self, request):
        self.addcredentials(request)
        return  HttpTransport.send(self, request)
    
    def addcredentials(self, request):
        credentials = self.credentials()
        if not (None in credentials):
            u = credentials[0]
            p = credentials[1]
            self.pm.add_password(None, request.url, u, p)
    
    def credentials(self):
        return (self.options.username, self.options.password)
    
    def u2handlers(self):
            handlers = HttpTransport.u2handlers(self)
            handlers.append(HTTPBasicAuthHandler(self.pm))
            return handlers
示例#39
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)
示例#40
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)
示例#41
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
示例#42
0
    def get_opener(self):
        # The opener is yet build ?
        if self.opener is not None:
            return self.opener

        # Build a new opener
        opener = build_opener()
        headers = [ ('User-agent', 'restedit/%s' % __version__) ]

        # Add the "includes"
        for include in self.includes:
            headers.append(include)

        # An authentication ?
        auth_header = self.metadata.get('auth')
        if auth_header is not None:
            if auth_header.lower().startswith('basic'):
                cls_handler = HTTPBasicAuthHandler
                chal = auth_header[6:].strip()
                # Automatically find the username and the password
                username, password = decode_base64(chal).split(':', 1)
            elif auth_header.lower().startswith('digest'):
                cls_handler = HTTPDigestAuthHandler
                # Automatically find the username, but we must ask the password
                # XXX undocumented functions
                chal = parse_keqv_list(parse_http_list(auth_header[7:]))
                username = chal['username']
                password = askPassword(chal['realm'], username)
            else:
                raise NotImplemented

            password_mgr = HTTPPasswordMgrWithDefaultRealm()
            password_mgr.add_password(realm=None,
                                      uri=self.url,
                                      user=username,
                                      passwd=password)

            auth_handler = cls_handler(password_mgr)
            opener.add_handler(auth_handler)

        # A cookie ?
        if self.metadata.get('cookie'):
            headers.append( ('Cookie', self.metadata['cookie']) )

        # All OK
        opener.addheaders = headers
        self.opener = opener
        return opener
示例#43
0
def run_query(search_terms):
    root_url = "https://api.datamarket.azure.com/Bing/Search/"
    source = "Web"
    results_per_page = 10
    offset = 0

    # Wrap quotes around our query terms as required by the Bing API.
    # The query we will then use is stored within variable query.
    query = "'{0}'".format(search_terms)
    query = quote(query)

    # Construct the latter part of our request's URL.
    # Sets the format of the response to JSON and sets other properties.
    search_url = "{0}{1}?$format=json&$top={2}&$skip={3}&Query={4}".format(
        root_url, source, results_per_page, offset, query
    )

    # Setup authentication with the Bing servers.
    # The username MUST be a blank string, and put in your API key!
    username = ""

    # Create a 'password manager' which handles authentication for us.
    password_mgr = HTTPPasswordMgrWithDefaultRealm()
    password_mgr.add_password(None, search_url, username, BING_API_KEY)

    # Create our results list which we'll populate.
    results = []
    print(search_url)
    try:
        # Prepare for connecting to Bing's servers.
        handler = HTTPBasicAuthHandler(password_mgr)
        opener = build_opener(handler)
        install_opener(opener)

        # Connect to the server and read the response generated.
        response = urlopen(search_url).read().decode(encoding="utf-8").replace("<", "")
        # print(response)
        json_response = json.loads(response, parse_int=True)

        # Loop through each page returned, populating out results list.
        for result in json_response["d"]["results"]:
            results.append({"title": result["Title"], "link": result["Url"], "summary": result["Description"]})

    except URLError as e:
        print("Error when querying the Bing API: ", e)

    return results
示例#44
0
    def push_to_web_interface(self, json_body_dict):
        destination_url = urljoin(self.notification_server_host, URL_PUSH_PATH)
        data = json.dumps(json_body_dict).encode("utf-8")

        passman = HTTPPasswordMgrWithDefaultRealm()
        un = self.notification_server_username
        pw = self.notification_server_password
        top_level_url = self.notification_server_host
        passman.add_password(None, top_level_url, un, pw)

        auth_handler = HTTPBasicAuthHandler(passman)
        opener = build_opener(auth_handler)
        opener.open(top_level_url)
        install_opener(opener)

        request = Request(destination_url, data=data, headers={'Content-Type': 'application/json',
                                                               'User-Agent': self.user_agent})
        opener.open(request)
def curl(url, params=None, auth=None, req_type="GET", data=None, headers=None):
  post_req = ["POST", "PUT"]
  get_req = ["GET", "DELETE"]

  if params is not None:
    url += "?" + urlencode(params)

  if req_type not in post_req + get_req:
    raise IOError("Wrong request type \"%s\" passed" % req_type)

  _headers = {}
  handler_chain = []

  if auth is not None:
    manager = HTTPPasswordMgrWithDefaultRealm()
    manager.add_password(None, url, auth["user"], auth["pass"])
    handler_chain.append(HTTPBasicAuthHandler(manager))

  if req_type in post_req and data is not None:
    _headers["Content-Length"] = len(data)

  if headers is not None:
    _headers.update(headers)

  director = build_opener(*handler_chain)

  if req_type in post_req:
    if sys.version_info.major == 3:
      _data = bytes(data, encoding='utf8')
    else:
      _data = bytes(data)

    req = Request(url, headers=_headers, data=_data)
  else:
    req = Request(url, headers=_headers)

  req.get_method = lambda: req_type
  result = director.open(req)

  return {
    "httpcode": result.code,
    "headers": result.info(),
    "content": result.read()
  }
示例#46
0
def _netrc_open(uri, filename=None):
    '''
    open uri using netrc credentials.

    :param uri: uri to open
    :param filename: optional, path to non-default netrc config file
    :returns: file-like object from opening a socket to uri, or None
    :raises IOError: if opening .netrc file fails (unless file not found)
    '''
    if not uri:
        return None
    parsed_uri = urlparse(uri)
    machine = parsed_uri.netloc
    if not machine:
        return None
    opener = None
    try:
        info = netrc.netrc(filename).authenticators(machine)
        if info is not None:
            (username, _ , password) = info
            if username and password:
                pass_man = HTTPPasswordMgrWithDefaultRealm()
                pass_man.add_password(None, machine, username, password)
                authhandler = HTTPBasicAuthHandler(pass_man)
                opener = build_opener(authhandler)
                return opener.open(uri)
        else:
            # caught below, like other netrc parse errors
            raise netrc.NetrcParseError('No authenticators for "%s"' % machine)
    except IOError as ioe:
        if ioe.errno != 2:
            # if = 2, User probably has no .netrc, this is not an error
            raise
    except netrc.NetrcParseError as neterr:
        logger = logging.getLogger('vcstools')
        logger.warn('WARNING: parsing .netrc: %s' % str(neterr))
    # we could install_opener() here, but prefer to keep
    # default opening clean. Client can do that, though.
    return None
示例#47
0
def urlopener_with_auth(url):
    """
    Given a URL, return the URL with auth stripped, and a urlopener that can
    open it.
    Uses urllib2, so SSL certs aren't verified.
    """
    opener = build_opener()
    parsed = urlparse(url)
    if parsed.username and parsed.password:
        host = parsed.hostname
        if parsed.port:
            host += ':%i' % parsed.port
        stripped_auth = (parsed[0], host) + parsed[2:]
        url = urlunparse(stripped_auth)

        passwd_mgr = HTTPPasswordMgrWithDefaultRealm()
        base_url = urlunparse((parsed[0], host, '', '', '', ''))
        passwd_mgr.add_password(realm=None, uri=base_url,
                                user=parsed.username, passwd=parsed.password)
        auth_handler = HTTPBasicAuthHandler(passwd_mgr)
        opener = build_opener(auth_handler)
    return url, opener
示例#48
0
文件: fetch.py 项目: lethliel/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)
示例#49
0
 def __init__(self, **kwargs):
     """
     @param kwargs: Keyword arguments.
         - B{proxy} - An http proxy to be specified on requests.
              The proxy is defined as {protocol:proxy,}
                 - type: I{dict}
                 - default: {}
         - B{timeout} - Set the url open timeout (seconds).
                 - type: I{float}
                 - default: 90
         - B{username} - The username used for http authentication.
                 - type: I{str}
                 - default: None
         - B{password} - The password used for http authentication.
                 - type: I{str}
                 - default: None
     """
     HttpTransport.__init__(self, **kwargs)
     self.pm = HTTPPasswordMgrWithDefaultRealm()
示例#50
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))
示例#51
0
def curl(url, params=None, auth=None, req_type='GET', data=None, headers=None, timeout=None, use_gzip=True):
  """
  Make request to web resource

  :param url: Url to endpoint
  :param params: list of params after "?"
  :param auth: authorization tokens
  :param req_type: column_type of the request
  :param data: data which need to be posted
  :param headers: headers which would be posted with request
  :param timeout: Request timeout
  :param use_gzip: Accept gzip and deflate response from the server
  :return Response object

  :column_type url str
  :column_type params dict
  :column_type auth CURLAuth
  :column_type req_type str
  :column_type headers dict
  :column_type timeout int
  :column_type use_gzip bool
  :rtype CURLResponse
  """
  post_req = ["POST", "PUT"]
  get_req = ["GET", "DELETE"]

  if params is not None:
    url += "?" + urlencode(params)

  if req_type not in post_req + get_req:
    raise IOError("Wrong request column_type \"%s\" passed" % req_type)

  _headers = {}
  handler_chain = []
  req_args = {
    "headers": _headers
  }
  # process content
  if req_type in post_req and data is not None:
    _data, __header = __parse_content(data)
    _headers.update(__header)
    _headers["Content-Length"] = len(_data)
    req_args["data"] = _data

  # process gzip and deflate
  if use_gzip:
    if "Accept-Encoding" in _headers:
      if "gzip" not in _headers["Accept-Encoding"]:
        _headers["Accept-Encoding"] += ", gzip, x-gzip, deflate"
    else:
      _headers["Accept-Encoding"] = "gzip, x-gzip, deflate"

  if auth is not None and auth.force is False:
    manager = HTTPPasswordMgrWithDefaultRealm()
    manager.add_password(None, url, auth.user, auth.password)
    handler_chain.append(HTTPBasicAuthHandler(manager))

  if auth is not None and auth.force:
    _headers.update(auth.headers)

  if headers is not None:
    _headers.update(headers)

  director = build_opener(*handler_chain)
  req = Request(url, **req_args)
  req.get_method = lambda: req_type

  try:
    if timeout is not None:
      return CURLResponse(director.open(req, timeout=timeout))
    else:
      return CURLResponse(director.open(req))
  except URLError as e:
    if isinstance(e, HTTPError):
      raise e
    else:
      raise TimeoutError
示例#52
0
def netapp_filer(host, username, password, timeout, command, parameters=None,
                 ssl=False):
    """
    Issue a command to the NetApp filer.
    Note: Change to default ssl on before we ship a release version.
    """
    proto = 'http'
    if ssl:
        proto = 'https'

    url = "%s://%s/servlets/netapp.servlets.admin.XMLrequest_filer" % \
          (proto, host)

    req = Request(url)
    req.add_header('Content-Type', 'text/xml')

    password_manager = HTTPPasswordMgrWithDefaultRealm()
    password_manager.add_password(None, url, username, password)
    auth_manager = HTTPBasicAuthHandler(password_manager)

    opener = build_opener(auth_manager)
    install_opener(opener)

    # build the command and the arguments for it
    p = ""

    if parameters:
        for k, v in list(parameters.items()):
            p += "<%s>%s</%s>" % (k, param_value(v), k)

    payload = "<%s>\n%s\n</%s>" % (command, p, command)

    data = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE netapp SYSTEM "file:/etc/netapp_filer.dtd">
<netapp xmlns="http://www.netapp.com/filer/admin" version="1.1">
%s
</netapp>
""" % payload

    handler = None
    rc = None
    try:
        handler = urlopen(req, data.encode('utf-8'), float(timeout))

        if handler.getcode() == 200:
            rc = netapp_filer_parse_response(handler.read())
    except HTTPError:
        raise
    except URLError as ue:
        if isinstance(ue.reason, socket.timeout):
            raise FilerError(Filer.ETIMEOUT, "Connection timeout")
        else:
            raise
    except socket.timeout:
        raise FilerError(Filer.ETIMEOUT, "Connection timeout")
    except SSLError as sse:
        # The ssl library doesn't give a good way to find specific reason.
        # We are doing a string contains which is not ideal, but other than
        # throwing a generic error in this case there isn't much we can do
        # to be more specific.
        if "timed out" in str(sse).lower():
            raise FilerError(Filer.ETIMEOUT, "Connection timeout (SSL)")
        else:
            raise FilerError(Filer.EUNKNOWN,
                             "SSL error occurred (%s)", str(sse))
    finally:
        if handler:
            handler.close()

    return rc
    def download(self, url, error_message, timeout, tries):
        """
        Downloads a URL and returns the contents

        Uses the proxy settings from the Package Control.sublime-settings file,
        however there seem to be a decent number of proxies that this code
        does not work with. Patches welcome!

        :param url:
            The URL to download

        :param error_message:
            A string to include in the console error that is printed
            when an error occurs

        :param timeout:
            The int number of seconds to set the timeout to

        :param tries:
            The int number of times to try and download the URL in the case of
            a timeout or HTTP 503 error

        :return:
            The string contents of the URL, or False on error
        """

        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]
        if os.name == 'nt':
            ntlm_auth_handler = ProxyNtlmAuthHandler(password_manager)
            handlers.append(ntlm_auth_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)
            if not bundle_path:
                return False
            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))
        install_opener(build_opener(*handlers))

        while tries > 0:
            tries -= 1
            try:
                request = Request(url, headers={
                    "User-Agent": self.settings.get('user_agent'),
                    # Don't be alarmed if the response from the server does not
                    # select one of these since the server runs a relatively new
                    # version of OpenSSL which supports compression on the SSL
                    # layer, and Apache will use that instead of HTTP-level
                    # encoding.
                    "Accept-Encoding": "gzip,deflate"})
                http_file = urlopen(request, timeout=timeout)
                self.handle_rate_limit(http_file, url)
                result = http_file.read()
                encoding = http_file.headers.get('Content-Encoding')
                return self.decode_response(encoding, result)

            except (HTTPException) as e:
                error_string = u'%s HTTP exception %s (%s) downloading %s.' % (
                    error_message, e.__class__.__name__, unicode_from_os(e), url)
                console_write(error_string, True)

            except (HTTPError) as e:
                # Make sure we obey Github's rate limiting headers
                self.handle_rate_limit(e, url)

                # Bitbucket and Github return 503 a decent amount
                if unicode_from_os(e.code) == '503':
                    error_string = u'Downloading %s was rate limited, trying again' % url
                    console_write(error_string, True)
                    continue

                error_string = u'%s HTTP error %s downloading %s.' % (
                    error_message, unicode_from_os(e.code), url)
                console_write(error_string, True)

            except (URLError) as e:

                # Bitbucket and Github timeout a decent amount
                if unicode_from_os(e.reason) == 'The read operation timed out' \
                        or unicode_from_os(e.reason) == 'timed out':
                    error_string = u'Downloading %s timed out, trying again' % url
                    console_write(error_string, True)
                    continue

                error_string = u'%s URL error %s downloading %s.' % (
                    error_message, unicode_from_os(e.reason), url)
                console_write(error_string, True)

            break
        return False
    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
                      http_proxy: %s
                      https_proxy: %s
                      proxy_username: %s
                      proxy_password: %s
                    ''',
                    (http_proxy, https_proxy, proxy_username, proxy_password)
                )

            secure_url_match = re.match('^https://([^/]+)', url)
            if secure_url_match is not None:
                bundle_path = get_ca_bundle_path(self.settings)
                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)
示例#55
0
文件: req1.py 项目: Machi427/python
from urllib.request import Request
from urllib.request import HTTPPasswordMgrWithDefaultRealm
from urllib.request import HTTPBasicAuthHandler
from urllib.request import build_opener
from urllib.request import install_opener
from urllib.request import urlopen

req = Request("http://example.com/")

password_manager = HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, "http://example.com/", 'user', 'pass')
auth_manager = HTTPBasicAuthHandler(password_manager)
opener = build_opener(auth_manager)
install_opener(opener)

handler = urlopen(req)

print(handler.getcode())

示例#56
0
def basic_auth_opener(url, username, password):
    password_manager = HTTPPasswordMgrWithDefaultRealm()
    password_manager.add_password(None, url, username, password)
    auth_handler = PreemptiveBasicAuthHandler(password_manager)
    opener = build_opener(auth_handler)
    return opener
示例#57
0
# 发送登陆信息
from urllib.request import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, build_opener
from urllib.request import URLError, Request, urlopen

username = "******"
password = "******"
url = "http://localhost:5000/"
# 创建一个密码管理对象,用来保存 HTTP 请求相关的用户名和密码.
p = HTTPPasswordMgrWithDefaultRealm()
p.add_password(None, url, username, password)
# HTTPBasicAuthHandler 管理认证
auth_handler = HTTPBasicAuthHandler(p)
opener = build_opener(auth_handler)


try:
    '''
    result = opener.open(url)
    html = result.read().decode("utf-8")
    print(html)
    '''
    response = urlopen(url)
    print(response.read().decode("utf-8"))

except URLError as e:
    print(e.reason)