當前位置: 首頁>>代碼示例>>Python>>正文


Python request.OpenerDirector類代碼示例

本文整理匯總了Python中urllib.request.OpenerDirector的典型用法代碼示例。如果您正苦於以下問題:Python OpenerDirector類的具體用法?Python OpenerDirector怎麽用?Python OpenerDirector使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了OpenerDirector類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_processors

    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.assertIsInstance(args[0], Request)
            else:
                # *_response
                self.assertEqual((handler, name), calls[i])
                self.assertEqual(len(args), 2)
                self.assertIsInstance(args[0], Request)
                # response from opener.open is None, because there's no
                # handler that defines http_open to handle it
                self.assertTrue(args[1] is None or
                             isinstance(args[1], MockResponse))
開發者ID:pogigroo,項目名稱:py3k-__format__,代碼行數:34,代碼來源:test_urllib2.py

示例2: test_http_error

    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:pogigroo,項目名稱:py3k-__format__,代碼行數:26,代碼來源:test_urllib2.py

示例3: test_handled

    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:pogigroo,項目名稱:py3k-__format__,代碼行數:25,代碼來源:test_urllib2.py

示例4: test_proxy_no_proxy

 def test_proxy_no_proxy(self):
     os.environ['no_proxy'] = 'python.org'
     o = OpenerDirector()
     ph = urllib.request.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:pogigroo,項目名稱:py3k-__format__,代碼行數:14,代碼來源:test_urllib2.py

示例5: test_basic_auth

 def test_basic_auth(self, quote_char='"'):
     opener = OpenerDirector()
     password_manager = MockPasswordManager()
     auth_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
     realm = "ACME Widget Store"
     http_handler = MockHTTPHandler(
         401, 'WWW-Authenticate: Basic realm=%s%s%s\r\n\r\n' %
         (quote_char, realm, quote_char) )
     opener.add_handler(auth_handler)
     opener.add_handler(http_handler)
     self._test_basic_auth(opener, auth_handler, "Authorization",
                           realm, http_handler, password_manager,
                           "http://acme.example.com/protected",
                           "http://acme.example.com/protected",
                           )
開發者ID:pogigroo,項目名稱:py3k-__format__,代碼行數:15,代碼來源:test_urllib2.py

示例6: test_proxy_basic_auth

 def test_proxy_basic_auth(self):
     opener = OpenerDirector()
     ph = urllib.request.ProxyHandler(dict(http="proxy.example.com:3128"))
     opener.add_handler(ph)
     password_manager = MockPasswordManager()
     auth_handler = urllib.request.ProxyBasicAuthHandler(password_manager)
     realm = "ACME Networks"
     http_handler = MockHTTPHandler(
         407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
     opener.add_handler(auth_handler)
     opener.add_handler(http_handler)
     self._test_basic_auth(opener, auth_handler, "Proxy-authorization",
                           realm, http_handler, password_manager,
                           "http://acme.example.com:3128/protected",
                           "proxy.example.com:3128",
                           )
開發者ID:pogigroo,項目名稱:py3k-__format__,代碼行數:16,代碼來源:test_urllib2.py

示例7: test_handler_order

    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:pogigroo,項目名稱:py3k-__format__,代碼行數:17,代碼來源:test_urllib2.py

示例8: save_to_file

def save_to_file(url: str, file_path: str, opener: OpenerDirector = None):
    def process(request_):
        with open(file_path, 'wb') as f:
            f.write(request_.read())

    if opener:
        with closing(opener.open(url)) as request:
            process(request)
    else:
        with closing(urlopen(url)) as request:
            process(request)
開發者ID:colajam93,項目名稱:aurpackager,代碼行數:11,代碼來源:download.py

示例9: fetch

    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:chris-martin,項目名稱:grouch,代碼行數:34,代碼來源:scraper.py

示例10: build_opener

def build_opener(*handlers, **kw):
    """Create an opener object from a list of handlers.

    The opener will use several default handlers, including support
    for HTTP and FTP.

    If any of the handlers passed as arguments are subclasses of the
    default handlers, the default handlers will not be used.
    """
    def isclass(obj):
        return isinstance(obj, class_type_) or hasattr(obj, "__bases__")

    opener = OpenerDirector()
    default_classes = [ProxyHandler, UnknownHandler, HTTPHandler,
                       HTTPDefaultErrorHandler, HTTPRedirectHandler,
                       FTPHandler, FileHandler, HTTPErrorProcessor]
    check_classes = list(default_classes)
    check_classes.append(HTTPSContextHandler)
    skip = []
    for klass in check_classes:
        for check in handlers:
            if isclass(check):
                if issubclass(check, klass):
                    skip.append(klass)
            elif isinstance(check, klass):
                skip.append(klass)

    for klass in default_classes:
        if klass not in skip:
            opener.add_handler(klass())
            
    # Pick up SSL context from keyword settings
    ssl_context = kw.get('ssl_context')
    
    # Add the HTTPS handler with ssl_context
    if HTTPSContextHandler not in skip:
        opener.add_handler(HTTPSContextHandler(ssl_context))

    for h in handlers:
        if isclass(h):
            h = h()
        opener.add_handler(h)

    return opener
開發者ID:18600597055,項目名稱:hue,代碼行數:44,代碼來源:urllib2_build_opener.py

示例11: super

            super().__init__(host='', port=0, timeout=timeout)
            self.socket_file = socket_file

        def connect(self):
            self.sock = socket.socket(family=socket.AF_UNIX, type=socket.SOCK_STREAM, proto=0, fileno=None)
            self.sock.settimeout(self.timeout)
            self.sock.connect(self.socket_file)

    def socket_open(self, req):
        socket_file, path = req.selector.split(':', 1)
        req.host = socket_file
        req.selector = path
        return self.do_open(self.SocketFileToHttpConnectionAdaptor, req)


better_urllib_get = OpenerDirector()
better_urllib_get.addheaders = DEFAULT_HEADERS.copy()
better_urllib_get.add_handler(HTTPHandler())
better_urllib_get.add_handler(HTTPSHandler())
better_urllib_get.add_handler(SocketFileHandler())


# Util functions
#############################################################################################


@lru_cache()
def get_url(url):
    response = better_urllib_get.open(url, timeout=timeout)
    return process_urllib_response(response), response.status
開發者ID:timdaman,項目名稱:check_docker,代碼行數:30,代碼來源:check_swarm.py

示例12: HTTPError

            raise HTTPError(full_url, 401, "Stopping Oauth2 failure loop for {}".format(full_url),
                            response.headers, response)

        auth_token = self._get_outh2_token(www_authenticate_header)

        request.add_unredirected_header('Authorization', 'Bearer ' + auth_token)
        return self.parent.open(request, timeout=request.timeout)


# Got some help from this example https://gist.github.com/FiloSottile/2077115
class HeadRequest(Request):
    def get_method(self):
        return "HEAD"


better_urllib_get = OpenerDirector()
better_urllib_get.addheaders = DEFAULT_HEADERS.copy()
better_urllib_get.add_handler(HTTPHandler())
better_urllib_get.add_handler(HTTPSHandler())
better_urllib_get.add_handler(HTTPRedirectHandler())
better_urllib_get.add_handler(SocketFileHandler())
better_urllib_get.add_handler(Oauth2TokenAuthHandler())


class RegistryError(Exception):
    def __init__(self, response):
        self.response_obj = response


# Util functions
#############################################################################################
開發者ID:teamwire,項目名稱:platform,代碼行數:31,代碼來源:check_docker.py

示例13: super

            super().__init__(host='', port=0, timeout=timeout)
            self.socket_file = socket_file

        def connect(self):
            self.sock = socket.socket(family=socket.AF_UNIX, type=socket.SOCK_STREAM, proto=0, fileno=None)
            self.sock.settimeout(self.timeout)
            self.sock.connect(self.socket_file)

    def socket_open(self, req):
        socket_file, path = req.selector.split(':')
        req.host = socket_file
        req.selector = path
        return self.do_open(self.SocketFileToHttpConnectionAdaptor, req)


better_urllib = OpenerDirector()
better_urllib.addheaders = [DEFAULT_HEADERS]
better_urllib.add_handler(HTTPHandler())
better_urllib.add_handler(HTTPSHandler())
better_urllib.add_handler(SocketFileHandler())

# Util functions
#############################################################################################
def parse_thresholds(spec):
    """
    Given a spec string break it up into ':' separated chunks. Convert strings to ints as it makes sense

    :param spec:
    :return: The thresholds, as intergers
    """
    parts = spec.split(':')
開發者ID:chenjun3092,項目名稱:check_docker-1,代碼行數:31,代碼來源:check_docker.py

示例14: __init__

 def __init__(self):
     OpenerDirector.__init__(self)
     self.recorded = []
開發者ID:pogigroo,項目名稱:py3k-__format__,代碼行數:3,代碼來源:test_urllib2.py

示例15: build_test_opener

def build_test_opener(*handler_instances):
    opener = OpenerDirector()
    for h in handler_instances:
        opener.add_handler(h)
    return opener
開發者ID:pogigroo,項目名稱:py3k-__format__,代碼行數:5,代碼來源:test_urllib2.py


注:本文中的urllib.request.OpenerDirector類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。