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


Python HTTPConnectionPool.request方法代码示例

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


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

示例1: test_timeout

# 需要导入模块: from urllib3 import HTTPConnectionPool [as 别名]
# 或者: from urllib3.HTTPConnectionPool import request [as 别名]
    def test_timeout(self):
        url = '/sleep?seconds=0.005'
        timeout = 0.001

        # Pool-global timeout
        pool = HTTPConnectionPool(self.host, self.port, timeout=timeout)

        conn = pool._get_conn()
        with self.assertRaises(SocketTimeout):
            pool._make_request(conn, 'GET', url)
        pool._put_conn(conn)

        with self.assertRaises(TimeoutError):
            pool.request('GET', url)

        # Request-specific timeout
        pool = HTTPConnectionPool(self.host, self.port, timeout=0.5)

        conn = pool._get_conn()
        with self.assertRaises(SocketTimeout):
            pool._make_request(conn, 'GET', url, timeout=timeout)
        pool._put_conn(conn)

        with self.assertRaises(TimeoutError):
            pool.request('GET', url, timeout=timeout)
开发者ID:PeterScott,项目名称:urllib3,代码行数:27,代码来源:test_connectionpool.py

示例2: test_connection_refused

# 需要导入模块: from urllib3 import HTTPConnectionPool [as 别名]
# 或者: from urllib3.HTTPConnectionPool import request [as 别名]
 def test_connection_refused(self):
     # Does the pool retry if there is no listener on the port?
     # Note: Socket server is not started until after the test.
     pool = HTTPConnectionPool(self.host, self.port)
     with self.assertRaises(MaxRetryError):
         pool.request('GET', '/')
     self._start_server(lambda x: None)
开发者ID:thatguystone,项目名称:urllib3,代码行数:9,代码来源:test_socketlevel.py

示例3: test_headers_are_sent_with_the_original_case

# 需要导入模块: from urllib3 import HTTPConnectionPool [as 别名]
# 或者: from urllib3.HTTPConnectionPool import request [as 别名]
    def test_headers_are_sent_with_the_original_case(self):
        headers = {"foo": "bar", "bAz": "quux"}
        parsed_headers = {}

        def socket_handler(listener):
            sock = listener.accept()[0]

            buf = b""
            while not buf.endswith(b"\r\n\r\n"):
                buf += sock.recv(65536)

            headers_list = [header for header in buf.split(b"\r\n")[1:] if header]

            for header in headers_list:
                (key, value) = header.split(b": ")
                parsed_headers[key.decode()] = value.decode()

            # Send incomplete message (note Content-Length)
            sock.send(("HTTP/1.1 204 No Content\r\n" "Content-Length: 0\r\n" "\r\n").encode("utf-8"))

            sock.close()

        self._start_server(socket_handler)
        expected_headers = {"Accept-Encoding": "identity", "Host": "{0}:{1}".format(self.host, self.port)}
        expected_headers.update(headers)

        pool = HTTPConnectionPool(self.host, self.port, retries=False)
        pool.request("GET", "/", headers=HTTPHeaderDict(headers))
        self.assertEqual(expected_headers, parsed_headers)
开发者ID:boyxuper,项目名称:urllib3,代码行数:31,代码来源:test_socketlevel.py

示例4: test_bad_connect

# 需要导入模块: from urllib3 import HTTPConnectionPool [as 别名]
# 或者: from urllib3.HTTPConnectionPool import request [as 别名]
 def test_bad_connect(self):
     pool = HTTPConnectionPool('badhost.invalid', self.port)
     try:
         pool.request('GET', '/', retries=5)
         self.fail("should raise timeout exception here")
     except MaxRetryError as e:
         self.assertTrue(isinstance(e.reason, ProtocolError), e.reason)
开发者ID:balagopalraj,项目名称:clearlinux,代码行数:9,代码来源:test_connectionpool.py

示例5: test_bad_connect

# 需要导入模块: from urllib3 import HTTPConnectionPool [as 别名]
# 或者: from urllib3.HTTPConnectionPool import request [as 别名]
 def test_bad_connect(self):
     pool = HTTPConnectionPool('badhost.invalid', self.port)
     try:
         pool.request('GET', '/', retries=5)
         self.fail("should raise timeout exception here")
     except MaxRetryError as e:
         self.assertEqual(type(e.reason), NewConnectionError)
开发者ID:Altynai,项目名称:urllib3,代码行数:9,代码来源:test_connectionpool.py

示例6: test_headers_are_sent_with_the_original_case

# 需要导入模块: from urllib3 import HTTPConnectionPool [as 别名]
# 或者: from urllib3.HTTPConnectionPool import request [as 别名]
    def test_headers_are_sent_with_the_original_case(self):
        headers = {'foo': 'bar', 'bAz': 'quux'}
        parsed_headers = {}

        def socket_handler(listener):
            sock = listener.accept()[0]

            buf = b''
            while not buf.endswith(b'\r\n\r\n'):
                buf += sock.recv(65536)

            headers_list = [header for header in buf.split(b'\r\n')[1:] if header]

            for header in headers_list:
                (key, value) = header.split(b': ')
                parsed_headers[key.decode('ascii')] = value.decode('ascii')

            sock.send((
                'HTTP/1.1 204 No Content\r\n'
                'Content-Length: 0\r\n'
                '\r\n').encode('utf-8'))

            sock.close()

        self._start_server(socket_handler)
        expected_headers = {'Accept-Encoding': 'identity',
                            'Host': '{0}:{1}'.format(self.host, self.port)}
        expected_headers.update(headers)

        pool = HTTPConnectionPool(self.host, self.port, retries=False)
        pool.request('GET', '/', headers=HTTPHeaderDict(headers))
        self.assertEqual(expected_headers, parsed_headers)
开发者ID:Altynai,项目名称:urllib3,代码行数:34,代码来源:test_socketlevel.py

示例7: test_timeout

# 需要导入模块: from urllib3 import HTTPConnectionPool [as 别名]
# 或者: from urllib3.HTTPConnectionPool import request [as 别名]
    def test_timeout(self):
        url = "/sleep?seconds=0.005"
        timeout = util.Timeout(read=0.001)

        # Pool-global timeout
        pool = HTTPConnectionPool(self.host, self.port, timeout=timeout)

        conn = pool._get_conn()
        self.assertRaises(ReadTimeoutError, pool._make_request, conn, "GET", url)
        pool._put_conn(conn)

        self.assertRaises(ReadTimeoutError, pool.request, "GET", url)

        # Request-specific timeouts should raise errors
        pool = HTTPConnectionPool(self.host, self.port, timeout=0.5)

        conn = pool._get_conn()
        self.assertRaises(ReadTimeoutError, pool._make_request, conn, "GET", url, timeout=timeout)
        pool._put_conn(conn)

        self.assertRaises(ReadTimeoutError, pool.request, "GET", url, timeout=timeout)

        # Timeout int/float passed directly to request and _make_request should
        # raise a request timeout
        self.assertRaises(ReadTimeoutError, pool.request, "GET", url, timeout=0.001)
        conn = pool._new_conn()
        self.assertRaises(ReadTimeoutError, pool._make_request, conn, "GET", url, timeout=0.001)
        pool._put_conn(conn)

        # Timeout int/float passed directly to _make_request should not raise a
        # request timeout if it's a high value
        pool.request("GET", url, timeout=5)
开发者ID:github3py,项目名称:urllib3,代码行数:34,代码来源:test_connectionpool.py

示例8: test_release_conn_parameter

# 需要导入模块: from urllib3 import HTTPConnectionPool [as 别名]
# 或者: from urllib3.HTTPConnectionPool import request [as 别名]
    def test_release_conn_parameter(self):
        MAXSIZE=5
        pool = HTTPConnectionPool(self.host, self.port, maxsize=MAXSIZE)
        self.assertEqual(pool.pool.qsize(), MAXSIZE)

        # Make request without releasing connection
        pool.request('GET', '/', release_conn=False, preload_content=False)
        self.assertEqual(pool.pool.qsize(), MAXSIZE-1)
开发者ID:kojit,项目名称:urllib3,代码行数:10,代码来源:test_connectionpool.py

示例9: test_timeout

# 需要导入模块: from urllib3 import HTTPConnectionPool [as 别名]
# 或者: from urllib3.HTTPConnectionPool import request [as 别名]
 def test_timeout(self):
     pool = HTTPConnectionPool(self.host, self.port, timeout=0.01)
     try:
         pool.request('GET', '/sleep',
                      fields={'seconds': '0.02'})
         self.fail("Failed to raise TimeoutError exception")
     except TimeoutError:
         pass
开发者ID:kojit,项目名称:urllib3,代码行数:10,代码来源:test_connectionpool.py

示例10: test_connection_error_retries

# 需要导入模块: from urllib3 import HTTPConnectionPool [as 别名]
# 或者: from urllib3.HTTPConnectionPool import request [as 别名]
 def test_connection_error_retries(self):
     """ ECONNREFUSED error should raise a connection error, with retries """
     port = find_unused_port()
     pool = HTTPConnectionPool(self.host, port)
     try:
         pool.request('GET', '/', retries=Retry(connect=3))
         self.fail("Should have failed with a connection error.")
     except MaxRetryError as e:
         self.assertEqual(type(e.reason), NewConnectionError)
开发者ID:Altynai,项目名称:urllib3,代码行数:11,代码来源:test_connectionpool.py

示例11: test_connection_count

# 需要导入模块: from urllib3 import HTTPConnectionPool [as 别名]
# 或者: from urllib3.HTTPConnectionPool import request [as 别名]
    def test_connection_count(self):
        pool = HTTPConnectionPool(self.host, self.port, maxsize=1)

        pool.request('GET', '/')
        pool.request('GET', '/')
        pool.request('GET', '/')

        self.assertEqual(pool.num_connections, 1)
        self.assertEqual(pool.num_requests, 3)
开发者ID:kojit,项目名称:urllib3,代码行数:11,代码来源:test_connectionpool.py

示例12: test_connection_count_bigpool

# 需要导入模块: from urllib3 import HTTPConnectionPool [as 别名]
# 或者: from urllib3.HTTPConnectionPool import request [as 别名]
    def test_connection_count_bigpool(self):
        http_pool = HTTPConnectionPool(self.host, self.port, maxsize=16)

        http_pool.request('GET', '/')
        http_pool.request('GET', '/')
        http_pool.request('GET', '/')

        self.assertEqual(http_pool.num_connections, 1)
        self.assertEqual(http_pool.num_requests, 3)
开发者ID:kojit,项目名称:urllib3,代码行数:11,代码来源:test_connectionpool.py

示例13: test_connection_error_retries

# 需要导入模块: from urllib3 import HTTPConnectionPool [as 别名]
# 或者: from urllib3.HTTPConnectionPool import request [as 别名]
 def test_connection_error_retries(self):
     """ ECONNREFUSED error should raise a connection error, with retries """
     port = find_unused_port()
     pool = HTTPConnectionPool(self.host, port)
     try:
         pool.request('GET', '/', retries=Retry(connect=3))
         self.fail("Should have failed with a connection error.")
     except MaxRetryError as e:
         self.assertTrue(isinstance(e.reason, ProtocolError))
         self.assertEqual(e.reason.args[1].errno, errno.ECONNREFUSED)
开发者ID:balagopalraj,项目名称:clearlinux,代码行数:12,代码来源:test_connectionpool.py

示例14: test_timeout_float

# 需要导入模块: from urllib3 import HTTPConnectionPool [as 别名]
# 或者: from urllib3.HTTPConnectionPool import request [as 别名]
    def test_timeout_float(self):
        block_event = Event()
        ready_event = self.start_basic_handler(block_send=block_event, num=2)

        # Pool-global timeout
        pool = HTTPConnectionPool(self.host, self.port, timeout=SHORT_TIMEOUT, retries=False)
        self.assertRaises(ReadTimeoutError, pool.request, 'GET', '/')
        block_event.set() # Release block

        # Shouldn't raise this time
        ready_event.wait()
        block_event.set() # Pre-release block
        pool.request('GET', '/')
开发者ID:rubycarlin,项目名称:PythonAppEngine-YouPlay,代码行数:15,代码来源:test_connectionpool.py

示例15: test_keepalive_close

# 需要导入模块: from urllib3 import HTTPConnectionPool [as 别名]
# 或者: from urllib3.HTTPConnectionPool import request [as 别名]
    def test_keepalive_close(self):
        # NOTE: This used to run against apache.org but it made the test suite
        # really slow and fail half the time. Setting it to skip until we can
        # make this run better locally.
        pool = HTTPConnectionPool(self.host, self.port,
                                  block=True, maxsize=1, timeout=2)

        r = pool.request('GET', '/keepalive?close=1', retries=0,
                         headers={
                             "Connection": "close",
                         })

        self.assertEqual(pool.num_connections, 1)

        # The dummyserver will have responded with Connection:close,
        # and httplib will properly cleanup the socket.

        # We grab the HTTPConnection object straight from the Queue,
        # because _get_conn() is where the check & reset occurs
        # pylint: disable-msg=W0212
        conn = pool.pool.get()
        self.assertEqual(conn.sock, None)
        pool._put_conn(conn)

        # Now with keep-alive
        r = pool.request('GET', '/keepalive?close=0', retries=0,
                         headers={
                             "Connection": "keep-alive",
                         })

        # The dummyserver responded with Connection:keep-alive, the connection
        # persists.
        conn = pool.pool.get()
        self.assertNotEqual(conn.sock, None)
        pool._put_conn(conn)

        # Another request asking the server to close the connection. This one
        # should get cleaned up for the next request.
        r = pool.request('GET', '/keepalive?close=1', retries=0,
                         headers={
                             "Connection": "close",
                         })

        self.assertEqual(r.status, 200)

        conn = pool.pool.get()
        self.assertEqual(conn.sock, None)
        pool._put_conn(conn)

        # Next request
        r = pool.request('GET', '/keepalive?close=0')
开发者ID:kojit,项目名称:urllib3,代码行数:53,代码来源:test_connectionpool.py


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