当前位置: 首页>>代码示例>>Python>>正文


Python HTTPSConnectionPool._new_conn方法代码示例

本文整理汇总了Python中urllib3.HTTPSConnectionPool._new_conn方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPSConnectionPool._new_conn方法的具体用法?Python HTTPSConnectionPool._new_conn怎么用?Python HTTPSConnectionPool._new_conn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在urllib3.HTTPSConnectionPool的用法示例。


在下文中一共展示了HTTPSConnectionPool._new_conn方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_enhanced_timeout

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import _new_conn [as 别名]
    def test_enhanced_timeout(self):
        import urllib3.connectionpool
        OriginalHTTPSConnection = urllib3.connectionpool.HTTPSConnection
        OriginalSSL = urllib3.connectionpool.ssl

        urllib3.connectionpool.ssl = None

        timeout = Timeout(connect=0.001)
        https_pool = HTTPSConnectionPool(TARPIT_HOST, self.port,
                                         timeout=timeout,
                                         cert_reqs='CERT_REQUIRED')
        conn = https_pool._new_conn()
        self.assertEqual(conn.__class__, HTTPSConnection)
        self.assertRaises(ConnectTimeoutError, https_pool.request, 'GET', '/')
        self.assertRaises(ConnectTimeoutError, https_pool._make_request, conn,
                          'GET', '/')

        timeout = Timeout(connect=5)
        https_pool = HTTPSConnectionPool(TARPIT_HOST, self.port,
                                         timeout=timeout,
                                         cert_reqs='CERT_REQUIRED')
        self.assertRaises(ConnectTimeoutError, https_pool.request, 'GET', '/',
                          timeout=Timeout(connect=0.001))

        timeout = Timeout(total=None)
        https_pool = HTTPSConnectionPool(TARPIT_HOST, self.port,
                                         timeout=timeout,
                                         cert_reqs='CERT_REQUIRED')
        conn = https_pool._new_conn()
        self.assertRaises(ConnectTimeoutError, https_pool.request, 'GET', '/',
                          timeout=Timeout(total=None, connect=0.001))

        https_pool = HTTPSConnectionPool(self.host, self.port,
                                         timeout=timeout,
                                         cert_reqs='CERT_NONE')
        conn = https_pool._new_conn()
        try:
            conn.set_tunnel(self.host, self.port)
        except AttributeError: # python 2.6
            conn._set_tunnel(self.host, self.port)
        conn._tunnel = mock.Mock()
        try:
            https_pool._make_request(conn, 'GET', '/')
        except AttributeError:
            # wrap_socket unavailable when you mock out ssl
            pass
        conn._tunnel.assert_called_once_with()

        # Undo
        urllib3.HTTPSConnection = OriginalHTTPSConnection
        urllib3.connectionpool.ssl = OriginalSSL
开发者ID:AngelMarquez,项目名称:urllib3,代码行数:53,代码来源:test_https.py

示例2: test_verified

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import _new_conn [as 别名]
    def test_verified(self):
        https_pool = HTTPSConnectionPool(self.host, self.port,
                                         cert_reqs='CERT_REQUIRED',
                                         ca_certs=DEFAULT_CA)
        self.addCleanup(https_pool.close)

        conn = https_pool._new_conn()
        self.assertEqual(conn.__class__, VerifiedHTTPSConnection)

        with mock.patch('warnings.warn') as warn:
            r = https_pool.request('GET', '/')
            self.assertEqual(r.status, 200)

            # Modern versions of Python, or systems using PyOpenSSL, don't
            # emit warnings.
            if sys.version_info >= (2, 7, 9) or util.IS_PYOPENSSL \
                    or util.IS_SECURETRANSPORT:
                self.assertFalse(warn.called, warn.call_args_list)
            else:
                self.assertTrue(warn.called)
                if util.HAS_SNI:
                    call = warn.call_args_list[0]
                else:
                    call = warn.call_args_list[1]
                error = call[0][1]
                self.assertEqual(error, InsecurePlatformWarning)
开发者ID:christophermca,项目名称:dotfiles,代码行数:28,代码来源:test_https.py

示例3: test_verified

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import _new_conn [as 别名]
    def test_verified(self):
        https_pool = HTTPSConnectionPool(self.host, self.port,
                                         cert_reqs='CERT_REQUIRED')

        conn = https_pool._new_conn()
        self.assertEqual(conn.__class__, VerifiedHTTPSConnection)

        self.assertRaises(SSLError, https_pool.request, 'GET', '/')

        https_pool.ca_certs = DEFAULT_CA_BAD

        try:
            https_pool.request('GET', '/')
            self.fail("Didn't raise SSL error with wrong CA")
        except SSLError as e:
            self.assertTrue('certificate verify failed' in str(e),
                            "Expected 'certificate verify failed',"
                            "instead got: %r" % e)

        https_pool.ca_certs = DEFAULT_CA
        https_pool.request('GET', '/')  # Should succeed without exceptions.

        https_fail_pool = HTTPSConnectionPool('127.0.0.1', self.port,
                                              cert_reqs='CERT_REQUIRED')
        https_fail_pool.ca_certs = DEFAULT_CA

        try:
            https_fail_pool.request('GET', '/')
            self.fail("Didn't raise SSL invalid common name")
        except SSLError as e:
            self.assertTrue("doesn't match" in str(e))
开发者ID:1T,项目名称:urllib3,代码行数:33,代码来源:test_https.py

示例4: test_verified_with_context

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import _new_conn [as 别名]
    def test_verified_with_context(self):
        ctx = util.ssl_.create_urllib3_context(cert_reqs=ssl.CERT_REQUIRED)
        ctx.load_verify_locations(cafile=DEFAULT_CA)
        https_pool = HTTPSConnectionPool(self.host, self.port,
                                         ssl_context=ctx)
        self.addCleanup(https_pool.close)

        conn = https_pool._new_conn()
        self.assertEqual(conn.__class__, VerifiedHTTPSConnection)

        with mock.patch('warnings.warn') as warn:
            r = https_pool.request('GET', '/')
            self.assertEqual(r.status, 200)

            # Modern versions of Python, or systems using PyOpenSSL, don't
            # emit warnings.
            if sys.version_info >= (2, 7, 9) or util.IS_PYOPENSSL:
                self.assertFalse(warn.called, warn.call_args_list)
            else:
                self.assertTrue(warn.called)
                if util.HAS_SNI:
                    call = warn.call_args_list[0]
                else:
                    call = warn.call_args_list[1]
                error = call[0][1]
                self.assertEqual(error, InsecurePlatformWarning)
开发者ID:Lukasa,项目名称:urllib3,代码行数:28,代码来源:test_https.py

示例5: test_verified

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import _new_conn [as 别名]
    def test_verified(self):
        https_pool = HTTPSConnectionPool(self.host, self.port,
                                         cert_reqs='CERT_REQUIRED',
                                         ca_certs=DEFAULT_CA)

        conn = https_pool._new_conn()
        self.assertEqual(conn.__class__, VerifiedHTTPSConnection)

        https_pool.request('GET', '/')  # Should succeed without exceptions.
开发者ID:iyed,项目名称:urllib3,代码行数:11,代码来源:test_https.py

示例6: test_verified

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import _new_conn [as 别名]
    def test_verified(self):
        https_pool = HTTPSConnectionPool(self.host, self.port, cert_reqs="CERT_REQUIRED")

        conn = https_pool._new_conn()
        self.assertEqual(conn.__class__, VerifiedHTTPSConnection)

        try:
            https_pool.request("GET", "/")
            self.fail("Didn't raise SSL error with no CA")
        except SSLError, e:
            self.assertIn("No root certificates", str(e))
开发者ID:poupas,项目名称:urllib3,代码行数:13,代码来源:test_https.py

示例7: test_verified

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import _new_conn [as 别名]
    def test_verified(self):
        https_pool = HTTPSConnectionPool(self.host, self.port,
                                         cert_reqs='CERT_REQUIRED')

        conn = https_pool._new_conn()
        self.assertEqual(conn.__class__, VerifiedHTTPSConnection)

        try:
            https_pool.request('GET', '/')
            self.fail("Didn't raise SSL error with no CA")
        except SSLError, e:
            self.assertTrue('No root certificates' in str(e))
开发者ID:blep,项目名称:urllib3,代码行数:14,代码来源:test_https.py

示例8: test_tunnel

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import _new_conn [as 别名]
 def test_tunnel(self):
     """ test the _tunnel behavior """
     timeout = Timeout(total=None)
     https_pool = HTTPSConnectionPool(self.host, self.port, timeout=timeout,
                                      cert_reqs='CERT_NONE')
     self.addCleanup(https_pool.close)
     conn = https_pool._new_conn()
     self.addCleanup(conn.close)
     conn.set_tunnel(self.host, self.port)
     conn._tunnel = mock.Mock()
     https_pool._make_request(conn, 'GET', '/')
     conn._tunnel.assert_called_once_with()
开发者ID:christophermca,项目名称:dotfiles,代码行数:14,代码来源:test_https.py

示例9: test_tunnel

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import _new_conn [as 别名]
 def test_tunnel(self):
     """ test the _tunnel behavior """
     timeout = Timeout(total=None)
     https_pool = HTTPSConnectionPool(self.host, self.port, timeout=timeout, cert_reqs="CERT_NONE")
     conn = https_pool._new_conn()
     try:
         conn.set_tunnel(self.host, self.port)
     except AttributeError:  # python 2.6
         conn._set_tunnel(self.host, self.port)
     conn._tunnel = mock.Mock()
     https_pool._make_request(conn, "GET", "/")
     conn._tunnel.assert_called_once_with()
开发者ID:gagern,项目名称:urllib3,代码行数:14,代码来源:test_https.py

示例10: test_verified

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import _new_conn [as 别名]
    def test_verified(self):
        https_pool = HTTPSConnectionPool(self.host, self.port,
                                         cert_reqs='CERT_REQUIRED',
                                         ca_certs=DEFAULT_CA)

        conn = https_pool._new_conn()
        self.assertEqual(conn.__class__, VerifiedHTTPSConnection)

        with mock.patch('warnings.warn') as warn:
            r = https_pool.request('GET', '/')
            self.assertEqual(r.status, 200)
            self.assertFalse(warn.called, warn.call_args_list)
开发者ID:JasonRepo,项目名称:urllib3,代码行数:14,代码来源:test_https.py

示例11: test_server_hostname

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import _new_conn [as 别名]
    def test_server_hostname(self):
        https_pool = HTTPSConnectionPool('127.0.0.1', self.port,
                                         cert_reqs='CERT_REQUIRED',
                                         ca_certs=DEFAULT_CA,
                                         server_hostname='localhost')
        self.addCleanup(https_pool.close)

        conn = https_pool._new_conn()
        conn.request('GET', '/')

        # Assert the wrapping socket is using the passed-through SNI name.
        # pyopenssl doesn't let you pull the server_hostname back off the
        # socket, so only add this assertion if the attribute is there (i.e.
        # the python ssl module).
        if hasattr(conn.sock, 'server_hostname'):
            self.assertEqual(conn.sock.server_hostname, "localhost")
开发者ID:christophermca,项目名称:dotfiles,代码行数:18,代码来源:test_https.py

示例12: test_verified

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import _new_conn [as 别名]
    def test_verified(self):
        https_pool = HTTPSConnectionPool(self.host, self.port, cert_reqs="CERT_REQUIRED", ca_certs=DEFAULT_CA)

        conn = https_pool._new_conn()
        self.assertEqual(conn.__class__, VerifiedHTTPSConnection)

        with mock.patch("warnings.warn") as warn:
            r = https_pool.request("GET", "/")
            self.assertEqual(r.status, 200)

            if sys.version_info >= (2, 7, 9):
                self.assertFalse(warn.called, warn.call_args_list)
            else:
                self.assertTrue(warn.called)
                call, = warn.call_args_list
                error = call[0][1]
                self.assertEqual(error, InsecurePlatformWarning)
开发者ID:balagopalraj,项目名称:clearlinux,代码行数:19,代码来源:test_https.py

示例13: test_ca_dir_verified

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import _new_conn [as 别名]
    def test_ca_dir_verified(self):
        https_pool = HTTPSConnectionPool(self.host, self.port,
                                         cert_reqs='CERT_REQUIRED',
                                         ca_cert_dir=DEFAULT_CA_DIR)
        self.addCleanup(https_pool.close)

        conn = https_pool._new_conn()
        self.assertEqual(conn.__class__, VerifiedHTTPSConnection)

        with mock.patch('warnings.warn') as warn:
            r = https_pool.request('GET', '/')
            self.assertEqual(r.status, 200)

            if sys.version_info >= (2, 7, 9):
                self.assertFalse(warn.called, warn.call_args_list)
            else:
                self.assertTrue(warn.called)
                call, = warn.call_args_list
                error = call[0][1]
                self.assertEqual(error, InsecurePlatformWarning)
开发者ID:Lukasa,项目名称:urllib3,代码行数:22,代码来源:test_https.py

示例14: test_verified

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import _new_conn [as 别名]
    def test_verified(self):
        https_pool = HTTPSConnectionPool(self.host, self.port,
                                         cert_reqs='CERT_REQUIRED',
                                         ca_certs=DEFAULT_CA)

        conn = https_pool._new_conn()
        self.assertEqual(conn.__class__, VerifiedHTTPSConnection)

        with mock.patch('warnings.warn') as warn:
            r = https_pool.request('GET', '/')
            self.assertEqual(r.status, 200)

            if sys.version_info >= (2, 7, 9) or util.IS_PYOPENSSL:
                self.assertFalse(warn.called, warn.call_args_list)
            else:
                self.assertTrue(warn.called)
                if util.HAS_SNI:
                    call = warn.call_args_list[0]
                else:
                    call = warn.call_args_list[1]
                error = call[0][1]
                self.assertEqual(error, InsecurePlatformWarning)
开发者ID:Altynai,项目名称:urllib3,代码行数:24,代码来源:test_https.py

示例15: TestHTTPS

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import _new_conn [as 别名]
class TestHTTPS(HTTPSDummyServerTestCase):
    def setUp(self):
        self._pool = HTTPSConnectionPool(self.host, self.port)

    def test_simple(self):
        r = self._pool.request('GET', '/')
        self.assertEqual(r.status, 200, r.data)

    def test_set_ssl_version_to_tlsv1(self):
        self._pool.ssl_version = ssl.PROTOCOL_TLSv1
        r = self._pool.request('GET', '/')
        self.assertEqual(r.status, 200, r.data)

    def test_verified(self):
        https_pool = HTTPSConnectionPool(self.host, self.port,
                                         cert_reqs='CERT_REQUIRED')

        conn = https_pool._new_conn()
        self.assertEqual(conn.__class__, VerifiedHTTPSConnection)

        self.assertRaises(SSLError, https_pool.request, 'GET', '/')

        https_pool.ca_certs = DEFAULT_CA_BAD

        try:
            https_pool.request('GET', '/')
            self.fail("Didn't raise SSL error with wrong CA")
        except SSLError as e:
            self.assertTrue('certificate verify failed' in str(e),
                            "Expected 'certificate verify failed',"
                            "instead got: %r" % e)

        https_pool.ca_certs = DEFAULT_CA
        https_pool.request('GET', '/')  # Should succeed without exceptions.

        https_fail_pool = HTTPSConnectionPool('127.0.0.1', self.port,
                                              cert_reqs='CERT_REQUIRED')
        https_fail_pool.ca_certs = DEFAULT_CA

        try:
            https_fail_pool.request('GET', '/')
            self.fail("Didn't raise SSL invalid common name")
        except SSLError as e:
            self.assertTrue("doesn't match" in str(e))

    def test_no_ssl(self):
        OriginalConnectionCls = self._pool.ConnectionCls
        try:
            self._pool.ConnectionCls = None

            self.assertRaises(SSLError, self._pool._new_conn)
            self.assertRaises(SSLError, self._pool.request, 'GET', '/')

        finally:
            self._pool.ConnectionCls = OriginalConnectionCls

    def test_unverified_ssl(self):
        """ Test that bare HTTPSConnection can connect, make requests """
        try:
            OriginalConnectionCls = self._pool.ConnectionCls
            self._pool.ConnectionCls = UnverifiedHTTPSConnection
            self._pool.request('GET', '/')

        finally:
            self._pool.ConnectionCls = OriginalConnectionCls

    def test_cert_reqs_as_constant(self):
        https_pool = HTTPSConnectionPool(self.host, self.port,
                                         cert_reqs=ssl.CERT_REQUIRED)

        https_pool.ca_certs = DEFAULT_CA_BAD
        # if we pass in an invalid value it defaults to CERT_NONE
        self.assertRaises(SSLError, https_pool.request, 'GET', '/')

    def test_cert_reqs_as_short_string(self):
        https_pool = HTTPSConnectionPool(self.host, self.port,
                                         cert_reqs='REQUIRED')

        https_pool.ca_certs = DEFAULT_CA_BAD
        # if we pass in an invalid value it defaults to CERT_NONE
        self.assertRaises(SSLError, https_pool.request, 'GET', '/')

    def test_ssl_unverified_with_ca_certs(self):
        https_pool = HTTPSConnectionPool(self.host, self.port,
                                         cert_reqs='CERT_NONE')

        https_pool.ca_certs = DEFAULT_CA_BAD
        https_pool.request('GET', '/')

    @requires_network
    def test_ssl_verified_with_platform_ca_certs(self):
        """
        This test check that whe rely on platform CA file to validate
        authenticity of SSL certificate. Since this file is used by many
        components of the OS, such as curl, apt-get, etc., we decided to not
        touch it, in order to not compromise the security of the OS
        running the test suite (typically urllib3 developer's OS).

        This test assume that httpbin.org use a certificate signed
        by a well known Certificate Authority.
#.........这里部分代码省略.........
开发者ID:1T,项目名称:urllib3,代码行数:103,代码来源:test_https.py


注:本文中的urllib3.HTTPSConnectionPool._new_conn方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。