• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python encoding.decode函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中netlib.encoding.decode函数的典型用法代码示例。如果您正苦于以下问题:Python decode函数的具体用法?Python decode怎么用?Python decode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: test_cache

def test_cache():
    decode_gzip = mock.MagicMock()
    decode_gzip.return_value = b"decoded"
    encode_gzip = mock.MagicMock()
    encode_gzip.return_value = b"encoded"

    with mock.patch.dict(encoding.custom_decode, gzip=decode_gzip):
        with mock.patch.dict(encoding.custom_encode, gzip=encode_gzip):
            assert encoding.decode(b"encoded", "gzip") == b"decoded"
            assert decode_gzip.call_count == 1

            # should be cached
            assert encoding.decode(b"encoded", "gzip") == b"decoded"
            assert decode_gzip.call_count == 1

            # the other way around as well
            assert encoding.encode(b"decoded", "gzip") == b"encoded"
            assert encode_gzip.call_count == 0

            # different encoding
            decode_gzip.return_value = b"bar"
            assert encoding.encode(b"decoded", "deflate") != b"decoded"
            assert encode_gzip.call_count == 0

            # This is not in the cache anymore
            assert encoding.encode(b"decoded", "gzip") == b"encoded"
            assert encode_gzip.call_count == 1
开发者ID:dufferzafar,项目名称:mitmproxy,代码行数:27,代码来源:test_encoding.py


示例2: test_identity

def test_identity():
    assert b"string" == encoding.decode("identity", b"string")
    assert b"string" == encoding.encode("identity", b"string")
    assert b"string" == encoding.encode(b"identity", b"string")
    assert b"string" == encoding.decode(b"identity", b"string")
    assert not encoding.encode("nonexistent", b"string")
    assert not encoding.decode("nonexistent encoding", b"string")
开发者ID:pombredanne,项目名称:netlib,代码行数:7,代码来源:test_encoding.py


示例3: test_gzip

def test_gzip():
    assert "string" == encoding.decode(
        "gzip",
        encoding.encode(
            "gzip",
            "string"))
    assert None == encoding.decode("gzip", "bogus")
开发者ID:gsilverman-memeo-inc,项目名称:netlib,代码行数:7,代码来源:test_encoding.py


示例4: test_gzip

def test_gzip():
    assert b"string" == encoding.decode(
        "gzip",
        encoding.encode(
            "gzip",
            b"string"
        )
    )
    assert encoding.decode("gzip", b"bogus") is None
开发者ID:pombredanne,项目名称:netlib,代码行数:9,代码来源:test_encoding.py


示例5: test_brotli

def test_brotli():
    assert b"string" == encoding.decode(
        encoding.encode(
            b"string",
            "br"
        ),
        "br"
    )
    with tutils.raises(ValueError):
        encoding.decode(b"bogus", "br")
开发者ID:Angelcold,项目名称:mitmproxy,代码行数:10,代码来源:test_encoding.py


示例6: test_gzip

def test_gzip():
    assert b"string" == encoding.decode(
        encoding.encode(
            b"string",
            "gzip"
        ),
        "gzip"
    )
    with tutils.raises(ValueError):
        encoding.decode(b"bogus", "gzip")
开发者ID:DrakeCaraker,项目名称:mitmproxy,代码行数:10,代码来源:test_encoding.py


示例7: test_deflate

def test_deflate():
    assert "string" == encoding.decode(
        "deflate",
        encoding.encode(
            "deflate",
            "string"))
    assert "string" == encoding.decode(
        "deflate",
        encoding.encode(
            "deflate",
            "string")[
            2:-
            4])
    assert None == encoding.decode("deflate", "bogus")
开发者ID:gsilverman-memeo-inc,项目名称:netlib,代码行数:14,代码来源:test_encoding.py


示例8: get_content_view

def get_content_view(viewmode, data, **metadata):
    """
        Args:
            viewmode: the view to use.
            data, **metadata: arguments passed to View instance.

        Returns:
            A (description, content generator) tuple.
            In contrast to calling the views directly, text is always safe-to-print unicode.

        Raises:
            ContentViewException, if the content view threw an error.
    """
    msg = []

    headers = metadata.get("headers", {})
    enc = headers.get("content-encoding")
    if enc and enc != "identity":
        decoded = encoding.decode(enc, data)
        if decoded:
            data = decoded
            msg.append("[decoded %s]" % enc)
    try:
        ret = viewmode(data, **metadata)
    # Third-party viewers can fail in unexpected ways...
    except Exception as e:
        six.reraise(exceptions.ContentViewException, exceptions.ContentViewException(str(e)), sys.exc_info()[2])
    if not ret:
        ret = get("Raw")(data, **metadata)
        msg.append("Couldn't parse: falling back to Raw")
    else:
        msg.append(ret[0])
    return " ".join(msg), safe_to_print(ret[1])
开发者ID:xushichang0,项目名称:mitmproxy,代码行数:33,代码来源:contentviews.py


示例9: test_deflate

def test_deflate():
    assert b"string" == encoding.decode(
        "deflate",
        encoding.encode(
            "deflate",
            b"string"
        )
    )
    assert b"string" == encoding.decode(
        "deflate",
        encoding.encode(
            "deflate",
            b"string"
        )[2:-4]
    )
    assert encoding.decode("deflate", b"bogus") is None
开发者ID:pombredanne,项目名称:netlib,代码行数:16,代码来源:test_encoding.py


示例10: get_text

    def get_text(self, strict=True):
        # type: (bool) -> six.text_type
        """
        The HTTP message body decoded with both content-encoding header (e.g. gzip)
        and content-type header charset.

        Raises:
            ValueError, when either content-encoding or charset is invalid and strict is True.

        See also: :py:attr:`content`, :py:class:`raw_content`
        """
        if self.raw_content is None:
            return None
        enc = self._guess_encoding()

        content = self.get_content(strict)
        cached = (
            self._text_cache.encoded == content and
            (self._text_cache.strict or not strict) and
            self._text_cache.encoding == enc
        )
        if not cached:
            is_strict = self._content_cache.strict
            try:
                decoded = encoding.decode(content, enc)
            except ValueError:
                if strict:
                    raise
                is_strict = False
                decoded = self.content.decode("utf8", "replace" if six.PY2 else "surrogateescape")
            self._text_cache = CachedDecode(content, enc, is_strict, decoded)
        return self._text_cache.decoded
开发者ID:DrakeCaraker,项目名称:mitmproxy,代码行数:32,代码来源:message.py


示例11: get_content

    def get_content(self, strict=True):
        # type: (bool) -> bytes
        """
        The HTTP message body decoded with the content-encoding header (e.g. gzip)

        Raises:
            ValueError, when the content-encoding is invalid and strict is True.

        See also: :py:class:`raw_content`, :py:attr:`text`
        """
        if self.raw_content is None:
            return None
        ce = self.headers.get("content-encoding")
        cached = (
            self._content_cache.encoded == self.raw_content and
            (self._content_cache.strict or not strict) and
            self._content_cache.encoding == ce
        )
        if not cached:
            is_strict = True
            if ce:
                try:
                    decoded = encoding.decode(self.raw_content, ce)
                except ValueError:
                    if strict:
                        raise
                    is_strict = False
                    decoded = self.raw_content
            else:
                decoded = self.raw_content
            self._content_cache = CachedDecode(self.raw_content, ce, is_strict, decoded)
        return self._content_cache.decoded
开发者ID:DrakeCaraker,项目名称:mitmproxy,代码行数:32,代码来源:message.py


示例12: get_content_view

def get_content_view(viewmode, headers, content, limit, is_request):
    """
        Returns a (msg, body) tuple.
    """
    if not content:
        if is_request:
            return "No request content (press tab to view response)", ""
        else:
            return "No content", ""
    msg = []

    enc = headers.get("content-encoding")
    if enc and enc != "identity":
        decoded = encoding.decode(enc, content)
        if decoded:
            content = decoded
            msg.append("[decoded %s]" % enc)
    try:
        ret = viewmode(headers, content, limit)
    # Third-party viewers can fail in unexpected ways...
    except Exception:
        s = traceback.format_exc()
        s = "Content viewer failed: \n" + s
        signals.add_event(s, "error")
        ret = None
    if not ret:
        ret = get("Raw")(headers, content, limit)
        msg.append("Couldn't parse: falling back to Raw")
    else:
        msg.append(ret[0])
    return " ".join(msg), ret[1]
开发者ID:camerony,项目名称:mitmproxy,代码行数:31,代码来源:contentview.py


示例13: test_deflate

def test_deflate():
    assert b"string" == encoding.decode(
        encoding.encode(
            b"string",
            "deflate"
        ),
        "deflate"
    )
    assert b"string" == encoding.decode(
        encoding.encode(
            b"string",
            "deflate"
        )[2:-4],
        "deflate"
    )
    with tutils.raises(ValueError):
        encoding.decode(b"bogus", "deflate")
开发者ID:DrakeCaraker,项目名称:mitmproxy,代码行数:17,代码来源:test_encoding.py


示例14: get_decoded_content

 def get_decoded_content(self):
     """
         Returns the decoded content based on the current Content-Encoding
         header.
         Doesn't change the message iteself or its headers.
     """
     ce = self.headers.get("content-encoding")
     if not self.content or ce not in encoding.ENCODINGS:
         return self.content
     return encoding.decode(ce, self.content)
开发者ID:karnex47,项目名称:mitmproxy,代码行数:10,代码来源:http.py


示例15: decode

    def decode(self):
        """
            Decodes body based on the current Content-Encoding header, then
            removes the header. If there is no Content-Encoding header, no
            action is taken.

            Returns:
                True, if decoding succeeded.
                False, otherwise.
        """
        ce = self.headers.get("content-encoding")
        data = encoding.decode(ce, self.content)
        if data is None:
            return False
        self.content = data
        self.headers.pop("content-encoding", None)
        return True
开发者ID:laborautonomo,项目名称:mitmproxy,代码行数:17,代码来源:message.py


示例16: decode

    def decode(self):
        """
            Decodes body based on the current Content-Encoding header, then
            removes the header. If there is no Content-Encoding header, no
            action is taken.

            Returns True if decoding succeeded, False otherwise.
        """
        ce = self.headers.get_first("content-encoding")
        if not self.body or ce not in encoding.ENCODINGS:
            return False
        data = encoding.decode(ce, self.body)
        if data is None:
            return False
        self.body = data
        del self.headers["content-encoding"]
        return True
开发者ID:noikiy,项目名称:mitmproxy,代码行数:17,代码来源:http_wrappers.py


示例17: get_text

    def get_text(self, strict=True):
        # type: (bool) -> six.text_type
        """
        The HTTP message body decoded with both content-encoding header (e.g. gzip)
        and content-type header charset.

        Raises:
            ValueError, when either content-encoding or charset is invalid and strict is True.

        See also: :py:attr:`content`, :py:class:`raw_content`
        """
        if self.raw_content is None:
            return None
        enc = self._guess_encoding()

        content = self.get_content(strict)
        try:
            return encoding.decode(content, enc)
        except ValueError:
            if strict:
                raise
            return content.decode("utf8", "replace" if six.PY2 else "surrogateescape")
开发者ID:dufferzafar,项目名称:mitmproxy,代码行数:22,代码来源:message.py


示例18: get_content

    def get_content(self, strict=True):
        # type: (bool) -> bytes
        """
        The HTTP message body decoded with the content-encoding header (e.g. gzip)

        Raises:
            ValueError, when the content-encoding is invalid and strict is True.

        See also: :py:class:`raw_content`, :py:attr:`text`
        """
        if self.raw_content is None:
            return None
        ce = self.headers.get("content-encoding")
        if ce:
            try:
                return encoding.decode(self.raw_content, ce)
            except ValueError:
                if strict:
                    raise
                return self.raw_content
        else:
            return self.raw_content
开发者ID:dufferzafar,项目名称:mitmproxy,代码行数:22,代码来源:message.py


示例19: test_encoders

def test_encoders(encoder):
    assert "" == encoding.decode("", encoder)
    assert b"" == encoding.decode(b"", encoder)

    assert "string" == encoding.decode(
        encoding.encode(
            "string",
            encoder
        ),
        encoder
    )
    assert b"string" == encoding.decode(
        encoding.encode(
            b"string",
            encoder
        ),
        encoder
    )

    with tutils.raises(ValueError):
        encoding.decode(b"foobar", encoder)
开发者ID:lordillusions,项目名称:mitmproxy,代码行数:21,代码来源:test_encoding.py


示例20: test_identity

def test_identity():
    assert b"string" == encoding.decode(b"string", "identity")
    assert b"string" == encoding.encode(b"string", "identity")
    with tutils.raises(ValueError):
        encoding.encode(b"string", "nonexistent encoding")
开发者ID:DrakeCaraker,项目名称:mitmproxy,代码行数:5,代码来源:test_encoding.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python http.parse_init_connect函数代码示例发布时间:2022-05-27
下一篇:
Python netjsonconfig.OpenWrt类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap