Beispiel #1
0
    def __call__(self, req):
        parsed = urllib_parse.urlparse(req.url)

        # Get the netloc without any embedded credentials
        netloc = parsed.netloc.rsplit("@", 1)[-1]

        # Set the url of the request to the url without any credentials
        req.url = urllib_parse.urlunparse(parsed[:1] + (netloc, ) + parsed[2:])

        # Use any stored credentials that we have for this netloc
        username, password = self.passwords.get(netloc, (None, None))

        # Extract credentials embedded in the url if we have none stored
        if username is None:
            username, password = self.parse_credentials(parsed.netloc)

        # Get creds from netrc if we still don't have them
        if username is None and password is None:
            netrc_auth = get_netrc_auth(req.url)
            username, password = netrc_auth if netrc_auth else (None, None)

        if username or password:
            # Store the username and password
            self.passwords[netloc] = (username, password)

            # Send the basic auth with this request
            req = HTTPBasicAuth(username or "", password or "")(req)

        # Attach a hook to handle 401 responses
        req.register_hook("response", self.handle_401)

        return req
Beispiel #2
0
 def request(self, host, handler, request_body, verbose=False):
     parts = (self._scheme, host, handler, None, None, None)
     url = urllib_parse.urlunparse(parts)
     try:
         headers = {'Content-Type': 'text/xml'}
         response = self._session.post(url,
                                       data=request_body,
                                       headers=headers,
                                       stream=True)
         response.raise_for_status()
         self.verbose = verbose
         return self.parse_response(response.raw)
     except requests.HTTPError as exc:
         logger.critical(
             "HTTP error %s while getting %s",
             exc.response.status_code,
             url,
         )
         raise