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


Python OpenerDirector.open方法代码示例

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


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

示例1: test_proxy_no_proxy

# 需要导入模块: from urllib2 import OpenerDirector [as 别名]
# 或者: from urllib2.OpenerDirector import open [as 别名]
 def test_proxy_no_proxy(self):
     os.environ['no_proxy'] = 'python.org'
     o = OpenerDirector()
     ph = urllib2.ProxyHandler(dict(http="proxy.example.com"))
     o.add_handler(ph)
     req = Request("http://www.perl.org/")
     self.assertEqual(req.get_host(), "www.perl.org")
     r = o.open(req)
     self.assertEqual(req.get_host(), "proxy.example.com")
     req = Request("http://www.python.org")
     self.assertEqual(req.get_host(), "www.python.org")
     r = o.open(req)
     self.assertEqual(req.get_host(), "www.python.org")
     del os.environ['no_proxy']
开发者ID:005,项目名称:gevent,代码行数:16,代码来源:test_urllib2.py

示例2: test_processors

# 需要导入模块: from urllib2 import OpenerDirector [as 别名]
# 或者: from urllib2.OpenerDirector import open [as 别名]
    def test_processors(self):
        # *_request / *_response methods get called appropriately
        o = OpenerDirector()
        meth_spec = [
            [("http_request", "return request"),
             ("http_response", "return response")],
            [("http_request", "return request"),
             ("http_response", "return response")],
            ]
        handlers = add_ordered_mock_handlers(o, meth_spec)

        req = Request("http://example.com/")
        r = o.open(req)
        # processor methods are called on *all* handlers that define them,
        # not just the first handler that handles the request
        calls = [
            (handlers[0], "http_request"), (handlers[1], "http_request"),
            (handlers[0], "http_response"), (handlers[1], "http_response")]

        for i, (handler, name, args, kwds) in enumerate(o.calls):
            if i < 2:
                # *_request
                self.assertEqual((handler, name), calls[i])
                self.assertEqual(len(args), 1)
                self.assert_(isinstance(args[0], Request))
            else:
                # *_response
                self.assertEqual((handler, name), calls[i])
                self.assertEqual(len(args), 2)
                self.assert_(isinstance(args[0], Request))
                # response from opener.open is None, because there's no
                # handler that defines http_open to handle it
                self.assert_(args[1] is None or
                             isinstance(args[1], MockResponse))
开发者ID:005,项目名称:gevent,代码行数:36,代码来源:test_urllib2.py

示例3: test_handled

# 需要导入模块: from urllib2 import OpenerDirector [as 别名]
# 或者: from urllib2.OpenerDirector import open [as 别名]
    def test_handled(self):
        # handler returning non-None means no more handlers will be called
        o = OpenerDirector()
        meth_spec = [
            ["http_open", "ftp_open", "http_error_302"],
            ["ftp_open"],
            [("http_open", "return self")],
            [("http_open", "return self")],
            ]
        handlers = add_ordered_mock_handlers(o, meth_spec)

        req = Request("http://example.com/")
        r = o.open(req)
        # Second .http_open() gets called, third doesn't, since second returned
        # non-None.  Handlers without .http_open() never get any methods called
        # on them.
        # In fact, second mock handler defining .http_open() returns self
        # (instead of response), which becomes the OpenerDirector's return
        # value.
        self.assertEqual(r, handlers[2])
        calls = [(handlers[0], "http_open"), (handlers[2], "http_open")]
        for expected, got in zip(calls, o.calls):
            handler, name, args, kwds = got
            self.assertEqual((handler, name), expected)
            self.assertEqual(args, (req,))
开发者ID:005,项目名称:gevent,代码行数:27,代码来源:test_urllib2.py

示例4: test_http_error

# 需要导入模块: from urllib2 import OpenerDirector [as 别名]
# 或者: from urllib2.OpenerDirector import open [as 别名]
    def test_http_error(self):
        # XXX http_error_default
        # http errors are a special case
        o = OpenerDirector()
        meth_spec = [
            [("http_open", "error 302")],
            [("http_error_400", "raise"), "http_open"],
            [("http_error_302", "return response"), "http_error_303",
             "http_error"],
            [("http_error_302")],
            ]
        handlers = add_ordered_mock_handlers(o, meth_spec)

        class Unknown:
            def __eq__(self, other): return True

        req = Request("http://example.com/")
        r = o.open(req)
        assert len(o.calls) == 2
        calls = [(handlers[0], "http_open", (req,)),
                 (handlers[2], "http_error_302",
                  (req, Unknown(), 302, "", {}))]
        for expected, got in zip(calls, o.calls):
            handler, method_name, args = expected
            self.assertEqual((handler, method_name), got[:2])
            self.assertEqual(args, got[2])
开发者ID:005,项目名称:gevent,代码行数:28,代码来源:test_urllib2.py

示例5: fetch

# 需要导入模块: from urllib2 import OpenerDirector [as 别名]
# 或者: from urllib2.OpenerDirector import open [as 别名]
    def fetch(self, request, opener=None, summary=None):

        if not self.__enable_http:
            return (None, None)

        if opener is None:
            opener = OpenerDirector()
            opener.add_handler(HTTPDefaultErrorHandler())
            opener.add_handler(HTTPSHandler())

        t = time.clock()
        response = opener.open(request)
        body = response.read()
        t = timedelta(seconds=time.clock() - t)
        url = request.get_full_url()
        self.__context.get_logger().info("HTTP time: %s\n%s" % (t, url))

        if self.__log_http:
            log_dir = os.path.join(self.__context.get_config_dir(), "http-log")
            makedirs(log_dir)
            log_file = os.path.join(log_dir, datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S-%f"))
            if summary is not None:
                log_file += "-" + _safe_str(summary)
            fp = open(log_file, "w")
            fp.write(
                "\n\n".join(
                    [request.get_full_url(), request.get_data() or "No request data", body or "No response body"]
                )
            )
            fp.close()

        return (response, body)
开发者ID:alecgorge,项目名称:grouch,代码行数:34,代码来源:scraper.py

示例6: test_proxy_https

# 需要导入模块: from urllib2 import OpenerDirector [as 别名]
# 或者: from urllib2.OpenerDirector import open [as 别名]
 def test_proxy_https(self):
     o = OpenerDirector()
     ph = urllib2.ProxyHandler(dict(https="proxy.example.com:3128"))
     o.add_handler(ph)
     meth_spec = [[("https_open", "return response")]]
     handlers = add_ordered_mock_handlers(o, meth_spec)
     req = Request("https://www.example.com/")
     self.assertEqual(req.get_host(), "www.example.com")
     r = o.open(req)
     self.assertEqual(req.get_host(), "proxy.example.com:3128")
     self.assertEqual([(handlers[0], "https_open")], [tup[0:2] for tup in o.calls])
开发者ID:int3,项目名称:jython,代码行数:13,代码来源:test_urllib2.py

示例7: test_handler_order

# 需要导入模块: from urllib2 import OpenerDirector [as 别名]
# 或者: from urllib2.OpenerDirector import open [as 别名]
    def test_handler_order(self):
        o = OpenerDirector()
        handlers = []
        for meths, handler_order in [
            ([("http_open", "return self")], 500),
            (["http_open"], 0),
            ]:
            class MockHandlerSubclass(MockHandler): pass
            h = MockHandlerSubclass(meths)
            h.handler_order = handler_order
            handlers.append(h)
            o.add_handler(h)

        r = o.open("http://example.com/")
        # handlers called in reverse order, thanks to their sort order
        self.assertEqual(o.calls[0][0], handlers[1])
        self.assertEqual(o.calls[1][0], handlers[0])
开发者ID:005,项目名称:gevent,代码行数:19,代码来源:test_urllib2.py

示例8: test_proxy_https_proxy_authorization

# 需要导入模块: from urllib2 import OpenerDirector [as 别名]
# 或者: from urllib2.OpenerDirector import open [as 别名]
 def test_proxy_https_proxy_authorization(self):
     o = OpenerDirector()
     ph = urllib2.ProxyHandler(dict(https="proxy.example.com:3128"))
     o.add_handler(ph)
     https_handler = MockHTTPSHandler()
     o.add_handler(https_handler)
     req = Request("https://www.example.com/")
     req.add_header("Proxy-Authorization", "FooBar")
     req.add_header("User-Agent", "Grail")
     self.assertEqual(req.get_host(), "www.example.com")
     self.assertIsNone(req._tunnel_host)
     r = o.open(req)
     # Verify Proxy-Authorization gets tunneled to request.
     # httpsconn req_headers do not have the Proxy-Authorization header but
     # the req will have.
     self.assertNotIn(("Proxy-Authorization", "FooBar"), https_handler.httpconn.req_headers)
     self.assertIn(("User-Agent", "Grail"), https_handler.httpconn.req_headers)
     self.assertIsNotNone(req._tunnel_host)
     self.assertEqual(req.get_host(), "proxy.example.com:3128")
     self.assertEqual(req.get_header("Proxy-authorization"), "FooBar")
开发者ID:int3,项目名称:jython,代码行数:22,代码来源:test_urllib2.py

示例9: WebTest

# 需要导入模块: from urllib2 import OpenerDirector [as 别名]
# 或者: from urllib2.OpenerDirector import open [as 别名]
class WebTest(TestCase):
    def setUp(self):
        self.server = StubServer(8998)
        self.server.run()

    def tearDown(self):
        self.server.stop()
        self.server.verify()  # this is redundant because stop includes verify

    def _make_request(self, url, method="GET", payload=None, headers={}):
        self.opener = OpenerDirector()
        self.opener.add_handler(HTTPHandler())
        if payload is None:
            request = Request(url, headers=headers)
        else:
            request = Request(url, headers=headers, data=payload.encode('utf-8'))
        request.get_method = lambda: method
        response = self.opener.open(request)
        response_code = getattr(response, 'code', -1)
        return (response, response_code)

    def test_get_with_file_call(self):
        with open('data.txt', 'w') as f:
            f.write("test file")
        self.server.expect(method="GET", url="/address/\d+$").and_return(mime_type="text/xml", file_content="./data.txt")
        response, response_code = self._make_request("http://localhost:8998/address/25", method="GET")
        with open("./data.txt", "r") as f:
            expected = f.read().encode('utf-8')
        try:
            self.assertEqual(expected, response.read())
        finally:
            response.close()

    def test_put_with_capture(self):
        capture = {}
        self.server.expect(method="PUT", url="/address/\d+$", data_capture=capture).and_return(reply_code=201)
        f, reply_code = self._make_request("http://localhost:8998/address/45", method="PUT", payload=str({"hello": "world", "hi": "mum"}))
        try:
            self.assertEqual(b"", f.read())
            captured = eval(capture["body"])
            self.assertEqual("world", captured["hello"])
            self.assertEqual("mum", captured["hi"])
            self.assertEqual(201, reply_code)
        finally:
            f.close()

    def test_post_with_wrong_data(self):
        self.server.expect(method="POST", url="data/", data='Bob').and_return()
        f, reply_code = self._make_request("http://localhost:8998/data/", method="POST", payload='Chris')
        self.assertEqual(403, reply_code)
        self.assertRaises(Exception, self.server.stop)

    def test_post_with_multiple_expectations_wrong_data(self):
        self.server.expect(method="POST", url="data/", data='Bob').and_return(reply_code=201)
        self.server.expect(method="POST", url="data/", data='John').and_return(reply_code=202)
        self.server.expect(method="POST", url="data/", data='Dave').and_return(reply_code=203)
        f, reply_code = self._make_request("http://localhost:8998/data/", method="POST", payload='Dave')
        self.assertEqual(203, reply_code)
        f, reply_code = self._make_request("http://localhost:8998/data/", method="POST", payload='Bob')
        self.assertEqual(201, reply_code)
        f, reply_code = self._make_request("http://localhost:8998/data/", method="POST", payload='Chris')
        self.assertEqual(403, reply_code)
        self.assertRaises(Exception, self.server.stop)

    def test_post_with_mixed_expectations(self):
        self.server.expect(method="POST", url="data/").and_return(reply_code=202)
        self.server.expect(method="POST", url="data/", data='John').and_return(reply_code=201)
        f, reply_code = self._make_request("http://localhost:8998/data/", method="POST", payload='John')
        self.assertEqual(201, reply_code)
        f, reply_code = self._make_request("http://localhost:8998/data/", method="POST", payload='Dave')
        self.assertEqual(202, reply_code)

    def test_post_with_data_and_no_body_response(self):
        self.server.expect(method="POST", url="address/\d+/inhabitant", data='<inhabitant name="Chris"/>').and_return(reply_code=204)
        f, reply_code = self._make_request("http://localhost:8998/address/45/inhabitant", method="POST", payload='<inhabitant name="Chris"/>')
        self.assertEqual(204, reply_code)

    def test_multiple_expectations_identifies_correct_unmatched_request(self):
        self.server.expect(method="POST", url="address/\d+/inhabitant", data='Twas brillig and the slithy toves').and_return(reply_code=204)
        f, reply_code = self._make_request("http://localhost:8998/address/45/inhabitant", method="POST", payload='Twas brillig and the slithy toves')
        self.assertEqual(204, reply_code)
        self.server.expect(method="GET", url="/monitor/server_status$").and_return(content="Four score and seven years ago", mime_type="text/html")
        try:
            self.server.stop()
        except Exception as e:
            self.assertEqual(-1, str(e).find('brillig'), str(e))

    def test_get_with_data(self):
        self.server.expect(method="GET", url="/monitor/server_status$").and_return(content="<html><body>Server is up</body></html>", mime_type="text/html")
        f, reply_code = self._make_request("http://localhost:8998/monitor/server_status", method="GET")
        try:
            self.assertTrue(b"Server is up" in f.read())
            self.assertEqual(200, reply_code)
        finally:
            f.close()

    def test_get_from_root(self):
        self.server.expect(method="GET", url="/$").and_return(content="<html><body>Server is up</body></html>", mime_type="text/html")
        f, reply_code = self._make_request("http://localhost:8998/", method="GET")
        try:
#.........这里部分代码省略.........
开发者ID:tarttelin,项目名称:Python-Stub-Server,代码行数:103,代码来源:test.py


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