requests.compat.urlparse.hostname

Here are the examples of the python api requests.compat.urlparse.hostname taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

13 Examples 7

Example 1

View license
    def test_generate_request_header_custom_service(self):
        with patch.multiple(kerberos_module_name,
                            authGSSClientInit=clientInit_complete,
                            authGSSClientResponse=clientResponse,
                            authGSSClientStep=clientStep_continue):
            response = requests.Response()
            response.url = "http://www.example.org/"
            response.headers = {'www-authenticate': 'negotiate token'}
            host = urlparse(response.url).hostname
            auth = requests_kerberos.HTTPKerberosAuth(service="barfoo")
            auth.generate_request_header(response, host),
            clientInit_complete.assert_called_with(
                "[email protected]",
                gssflags=(
                    kerberos.GSS_C_MUTUAL_FLAG |
                    kerberos.GSS_C_SEQUENCE_FLAG),
                principal=None)

Example 2

View license
    def test_principal_override(self):
        with patch.multiple(kerberos_module_name,
                            authGSSClientInit=clientInit_complete,
                            authGSSClientResponse=clientResponse,
                            authGSSClientStep=clientStep_continue):
            response = requests.Response()
            response.url = "http://www.example.org/"
            response.headers = {'www-authenticate': 'negotiate token'}
            host = urlparse(response.url).hostname
            auth = requests_kerberos.HTTPKerberosAuth(principal="[email protected]")
            auth.generate_request_header(response, host)
            clientInit_complete.assert_called_with(
                "[email protected]",
                gssflags=(
                    kerberos.GSS_C_MUTUAL_FLAG |
                    kerberos.GSS_C_SEQUENCE_FLAG),
                principal="[email protected]")

Example 3

View license
    def test_realm_override(self):
        with patch.multiple(kerberos_module_name,
                            authGSSClientInit=clientInit_complete,
                            authGSSClientResponse=clientResponse,
                            authGSSClientStep=clientStep_continue):
            response = requests.Response()
            response.url = "http://www.example.org/"
            response.headers = {'www-authenticate': 'negotiate token'}
            host = urlparse(response.url).hostname
            auth = requests_kerberos.HTTPKerberosAuth(hostname_override="otherhost.otherdomain.org")
            auth.generate_request_header(response, host)
            clientInit_complete.assert_called_with(
                "[email protected]",
                gssflags=(
                    kerberos.GSS_C_MUTUAL_FLAG |
                    kerberos.GSS_C_SEQUENCE_FLAG),
                principal=None)

Example 4

Project: planet-client-python
Source File: dispatch.py
View license
def _is_subdomain_of_tld(url1, url2):
    orig_host = urlparse(url1).hostname
    re_host = urlparse(url2).hostname
    return orig_host.split('.')[-2:] == re_host.split('.')[-2:]

Example 5

Project: requests-kerberos
Source File: kerberos_.py
View license
    def authenticate_user(self, response, **kwargs):
        """Handles user authentication with gssapi/kerberos"""

        host = urlparse(response.url).hostname

        try:
            auth_header = self.generate_request_header(response, host)
        except KerberosExchangeError:
            # GSS Failure, return existing response
            return response

        log.debug("authenticate_user(): Authorization header: {0}".format(
            auth_header))
        response.request.headers['Authorization'] = auth_header

        # Consume the content so we can reuse the connection for the next
        # request.
        response.content
        response.raw.release_conn()

        _r = response.connection.send(response.request, **kwargs)
        _r.history.append(response)

        log.debug("authenticate_user(): returning {0}".format(_r))
        return _r

Example 6

Project: requests-kerberos
Source File: kerberos_.py
View license
    def authenticate_server(self, response):
        """
        Uses GSSAPI to authenticate the server.

        Returns True on success, False on failure.
        """

        log.debug("authenticate_server(): Authenticate header: {0}".format(
            _negotiate_value(response)))

        host = urlparse(response.url).hostname

        try:
            result = kerberos.authGSSClientStep(self.context[host],
                                                _negotiate_value(response))
        except kerberos.GSSError:
            log.exception("authenticate_server(): authGSSClientStep() failed:")
            return False

        if result < 1:
            log.error("authenticate_server(): authGSSClientStep() failed: "
                      "{0}".format(result))
            return False

        log.debug("authenticate_server(): returning {0}".format(response))
        return True

Example 7

Project: requests-kerberos
Source File: kerberos_.py
View license
    def __call__(self, request):
        if self.force_preemptive:
            # add Authorization header before we receive a 401
            # by the 401 handler
            host = urlparse(request.url).hostname

            auth_header = self.generate_request_header(None, host, is_preemptive=True)

            log.debug("HTTPKerberosAuth: Preemptive Authorization header: {0}".format(auth_header))

            request.headers['Authorization'] = auth_header

        request.register_hook('response', self.handle_response)
        try:
            self.pos = request.body.tell()
        except AttributeError:
            # In the case of HTTPKerberosAuth being reused and the body
            # of the previous request was a file-like object, pos has
            # the file position of the previous body. Ensure it's set to
            # None.
            self.pos = None
        return request

Example 8

View license
    def test_generate_request_header(self):
        with patch.multiple(kerberos_module_name,
                            authGSSClientInit=clientInit_complete,
                            authGSSClientResponse=clientResponse,
                            authGSSClientStep=clientStep_continue):
            response = requests.Response()
            response.url = "http://www.example.org/"
            response.headers = {'www-authenticate': 'negotiate token'}
            host = urlparse(response.url).hostname
            auth = requests_kerberos.HTTPKerberosAuth()
            self.assertEqual(
                auth.generate_request_header(response, host),
                "Negotiate GSSRESPONSE"
            )
            clientInit_complete.assert_called_with(
                "[email protected]",
                gssflags=(
                    kerberos.GSS_C_MUTUAL_FLAG |
                    kerberos.GSS_C_SEQUENCE_FLAG),
                principal=None)
            clientStep_continue.assert_called_with("CTX", "token")
            clientResponse.assert_called_with("CTX")

Example 9

View license
    def test_generate_request_header_init_error(self):
        with patch.multiple(kerberos_module_name,
                            authGSSClientInit=clientInit_error,
                            authGSSClientResponse=clientResponse,
                            authGSSClientStep=clientStep_continue):
            response = requests.Response()
            response.url = "http://www.example.org/"
            response.headers = {'www-authenticate': 'negotiate token'}
            host = urlparse(response.url).hostname
            auth = requests_kerberos.HTTPKerberosAuth()
            self.assertRaises(requests_kerberos.exceptions.KerberosExchangeError,
                auth.generate_request_header, response, host
            )
            clientInit_error.assert_called_with(
                "[email protected]",
                gssflags=(
                    kerberos.GSS_C_MUTUAL_FLAG |
                    kerberos.GSS_C_SEQUENCE_FLAG),
                principal=None)
            self.assertFalse(clientStep_continue.called)
            self.assertFalse(clientResponse.called)

Example 10

View license
    def test_generate_request_header_step_error(self):
        with patch.multiple(kerberos_module_name,
                            authGSSClientInit=clientInit_complete,
                            authGSSClientResponse=clientResponse,
                            authGSSClientStep=clientStep_error):
            response = requests.Response()
            response.url = "http://www.example.org/"
            response.headers = {'www-authenticate': 'negotiate token'}
            host = urlparse(response.url).hostname
            auth = requests_kerberos.HTTPKerberosAuth()
            self.assertRaises(requests_kerberos.exceptions.KerberosExchangeError,
                auth.generate_request_header, response, host
            )
            clientInit_complete.assert_called_with(
                "[email protected]",
                gssflags=(
                    kerberos.GSS_C_MUTUAL_FLAG |
                    kerberos.GSS_C_SEQUENCE_FLAG),
                principal=None)
            clientStep_error.assert_called_with("CTX", "token")
            self.assertFalse(clientResponse.called)

Example 11

Project: WAPT
Source File: kerberos_.py
View license
    def authenticate_user(self, response, **kwargs):
        """Handles user authentication with gssapi/kerberos"""

        host = urlparse(response.url).hostname

        try:
            auth_header = self.generate_request_header(response, host)
        except KerberosExchangeError:
            # GSS Failure, return existing response
            return response

        log.debug("authenticate_user(): Authorization header: {0}".format(
            auth_header))
        response.request.headers['Authorization'] = auth_header

        # Consume the content so we can reuse the connection for the next
        # request.
        response.content
        response.raw.release_conn()

        _r = response.connection.send(response.request, **kwargs)
        _r.history.append(response)

        log.debug("authenticate_user(): returning {0}".format(_r))
        return _r

Example 12

Project: WAPT
Source File: kerberos_.py
View license
    def authenticate_server(self, response):
        """
        Uses GSSAPI to authenticate the server.

        Returns True on success, False on failure.
        """

        log.debug("authenticate_server(): Authenticate header: {0}".format(
            _negotiate_value(response)))

        host = urlparse(response.url).hostname

        try:
            result = kerberos.authGSSClientStep(self.context[host],
                                                _negotiate_value(response))
        except kerberos.GSSError:
            log.exception("authenticate_server(): authGSSClientStep() failed:")
            return False

        if result < 1:
            log.error("authenticate_server(): authGSSClientStep() failed: "
                      "{0}".format(result))
            return False

        log.debug("authenticate_server(): returning {0}".format(response))
        return True

Example 13

Project: WAPT
Source File: kerberos_.py
View license
    def __call__(self, request):
        if self.force_preemptive:
            # add Authorization header before we receive a 401
            # by the 401 handler
            host = urlparse(request.url).hostname

            auth_header = self.generate_request_header(None, host, is_preemptive=True)

            log.debug("HTTPKerberosAuth: Preemptive Authorization header: {0}".format(auth_header))

            request.headers['Authorization'] = auth_header

        request.register_hook('response', self.handle_response)
        try:
            self.pos = request.body.tell()
        except AttributeError:
            # In the case of HTTPKerberosAuth being reused and the body
            # of the previous request was a file-like object, pos has
            # the file position of the previous body. Ensure it's set to
            # None.
            self.pos = None
        return request