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

Python _compat.to_bytes函数代码示例

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

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



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

示例1: pbkdf2_bin

def pbkdf2_bin(data, salt, iterations=DEFAULT_PBKDF2_ITERATIONS,
               keylen=None, hashfunc=None):
    """Returns a binary digest for the PBKDF2 hash algorithm of `data`
    with the given `salt`. It iterates `iterations` times and produces a
    key of `keylen` bytes. By default, SHA-256 is used as hash function;
    a different hashlib `hashfunc` can be provided.

    .. versionadded:: 0.9

    :param data: the data to derive.
    :param salt: the salt for the derivation.
    :param iterations: the number of iterations.
    :param keylen: the length of the resulting key.  If not provided
                   the digest size will be used.
    :param hashfunc: the hash function to use.  This can either be the
                     string name of a known hash function or a function
                     from the hashlib module.  Defaults to sha256.
    """
    if not hashfunc:
        hashfunc = 'sha256'

    data = to_bytes(data)
    salt = to_bytes(salt)

    if callable(hashfunc):
        _test_hash = hashfunc()
        hash_name = getattr(_test_hash, 'name', None)
    else:
        hash_name = hashfunc
    return hashlib.pbkdf2_hmac(hash_name, data, salt, iterations, keylen)
开发者ID:gaoussoucamara,项目名称:simens-cerpad,代码行数:30,代码来源:security.py


示例2: pbkdf2_bin

def pbkdf2_bin(data, salt, iterations=DEFAULT_PBKDF2_ITERATIONS,
               keylen=None, hashfunc=None):
    """Returns a binary digest for the PBKDF2 hash algorithm of `data`
    with the given `salt`. It iterates `iterations` times and produces a
    key of `keylen` bytes. By default, SHA-1 is used as hash function;
    a different hashlib `hashfunc` can be provided.

    .. versionadded:: 0.9

    :param data: the data to derive.
    :param salt: the salt for the derivation.
    :param iterations: the number of iterations.
    :param keylen: the length of the resulting key.  If not provided
                   the digest size will be used.
    :param hashfunc: the hash function to use.  This can either be the
                     string name of a known hash function or a function
                     from the hashlib module.  Defaults to sha1.
    """
    if isinstance(hashfunc, string_types):
        hashfunc = _hash_funcs[hashfunc]
    elif not hashfunc:
        hashfunc = hashlib.sha1
    data = to_bytes(data)
    salt = to_bytes(salt)

    # If we're on Python with pbkdf2_hmac we can try to use it for
    # compatible digests.
    if _has_native_pbkdf2:
        _test_hash = hashfunc()
        if hasattr(_test_hash, 'name') and \
                        _test_hash.name in _hash_funcs:
            return hashlib.pbkdf2_hmac(_test_hash.name,
                                       data, salt, iterations,
                                       keylen)

    mac = hmac.HMAC(data, None, hashfunc)
    if not keylen:
        keylen = mac.digest_size

    def _pseudorandom(x, mac=mac):
        h = mac.copy()
        h.update(x)
        return bytearray(h.digest())

    buf = bytearray()
    for block in range_type(1, -(-keylen // mac.digest_size) + 1):
        rv = u = _pseudorandom(salt + _pack_int(block))
        for i in range_type(iterations - 1):
            u = _pseudorandom(bytes(u))
            rv = bytearray(starmap(xor, izip(rv, u)))
        buf.extend(rv)
    return bytes(buf[:keylen])
开发者ID:shakthydoss,项目名称:suriyan,代码行数:52,代码来源:security.py


示例3: tag

def tag(template, name):
    '''
    :param template:
        模板文件,此参数自动传入
    :param name:
        Tag名称,若为非ASCII字符,一般是经过URL编码的
    '''
    # 若name为非ASCII字符,传入时一般是经过URL编码的
    # 若name为URL编码,则需要解码为Unicode
    # URL编码判断方法:若已为URL编码, 再次编码会在每个码之前出现`%25`
    _name = to_bytes(name, 'utf-8')
    if urllib.quote(_name).count('%25') > 0:
        name = urllib.unquote(_name)

    tag = Tag.query.filter_by(name=name).first_or_404()
    
    page = int(request.args.get('page', 1))
    
    _url = PageURL(url_for('main.tag', name=name), {"page": page})
    _query = Article.query.public().filter(Article.tags.any(id=tag.id))
    pagination = Page(_query, page=page, items_per_page=Article.PER_PAGE, url=_url)

    articles = pagination.items

    _template = template % (tag.template or 'tag.html')
    return render_template(_template,
                           tag=tag, 
                           pagination=pagination,
                           articles=articles)
开发者ID:lxserenade,项目名称:wtxlog,代码行数:29,代码来源:views.py


示例4: test_dispatchermiddleware

def test_dispatchermiddleware():
    def null_application(environ, start_response):
        start_response('404 NOT FOUND', [('Content-Type', 'text/plain')])
        yield b'NOT FOUND'

    def dummy_application(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/plain')])
        yield to_bytes(environ['SCRIPT_NAME'])

    app = wsgi.DispatcherMiddleware(null_application, {
        '/test1': dummy_application,
        '/test2/very': dummy_application,
    })
    tests = {
        '/test1': ('/test1', '/test1/asfd', '/test1/very'),
        '/test2/very': ('/test2/very', '/test2/very/long/path/after/script/name')
    }
    for name, urls in tests.items():
        for p in urls:
            environ = create_environ(p)
            app_iter, status, headers = run_wsgi_app(app, environ)
            assert status == '200 OK'
            assert b''.join(app_iter).strip() == to_bytes(name)

    app_iter, status, headers = run_wsgi_app(
        app, create_environ('/missing'))
    assert status == '404 NOT FOUND'
    assert b''.join(app_iter).strip() == b'NOT FOUND'
开发者ID:geekKeen,项目名称:werkzeug,代码行数:28,代码来源:test_wsgi.py


示例5: quote

 def quote(cls, value):
     if cls.serialization_method is not None:
         value = cls.serialization_method.dumps(value)
         ### Added line
         value = to_bytes(value, 'utf-8')
     if cls.quote_base64:
         value = b''.join(base64.b64encode(value).splitlines()).strip()
     return value
开发者ID:jam-py,项目名称:jam-py,代码行数:8,代码来源:wsgi.py


示例6: __init__

 def __init__(self, data=None, secret_key=None, new=True):
     ModificationTrackingDict.__init__(self, data or ())
     # explicitly convert it into a bytestring because python 2.6
     # no longer performs an implicit string conversion on hmac
     if secret_key is not None:
         secret_key = to_bytes(secret_key, 'utf-8')
     self.secret_key = secret_key
     self.new = new
开发者ID:vipermark7,项目名称:code,代码行数:8,代码来源:securecookie.py


示例7: process_sql_params

def process_sql_params(params, cursor):
    result = []
    for p in params:
        if type(p) == tuple:
            value, data_type = p
            if data_type in [LONGTEXT, KEYS]:
                if type(value) == text_type:
                    value = to_bytes(value, 'utf-8')
        else:
            value = p
        result.append(value)
    return result
开发者ID:jam-py,项目名称:jam-py,代码行数:12,代码来源:firebird.py


示例8: test_multiple_cookies

def test_multiple_cookies():
    @Request.application
    def test_app(request):
        response = Response(repr(sorted(request.cookies.items())))
        response.set_cookie(u"test1", b"foo")
        response.set_cookie(u"test2", b"bar")
        return response

    client = Client(test_app, Response)
    resp = client.get("/")
    strict_eq(resp.data, b"[]")
    resp = client.get("/")
    strict_eq(resp.data, to_bytes(repr([("test1", u"foo"), ("test2", u"bar")]), "ascii"))
开发者ID:ajones620,项目名称:werkzeug,代码行数:13,代码来源:test_test.py


示例9: test_multiple_cookies

 def test_multiple_cookies(self):
     @Request.application
     def test_app(request):
         response = Response(repr(sorted(request.cookies.items())))
         response.set_cookie(u'test1', b'foo')
         response.set_cookie(u'test2', b'bar')
         return response
     client = Client(test_app, Response)
     resp = client.get('/')
     self.assert_strict_equal(resp.data, b'[]')
     resp = client.get('/')
     self.assert_strict_equal(resp.data,
                       to_bytes(repr([('test1', u'foo'), ('test2', u'bar')]), 'ascii'))
开发者ID:poffdeluxe,项目名称:werkzeug,代码行数:13,代码来源:test.py


示例10: quote

    def quote(cls, value):
        """Quote the value for the cookie.  This can be any object supported
        by :attr:`serialization_method`.

        :param value: the value to quote.
        """
        if cls.serialization_method is not None:
            value = cls.serialization_method.dumps(value)
        if cls.quote_base64:
            value = b''.join(
                base64.b64encode(to_bytes(value, "utf8")).splitlines()
            ).strip()
        return value
开发者ID:gaoussoucamara,项目名称:simens-cerpad,代码行数:13,代码来源:securecookie.py


示例11: __init__

    def __init__(self, servers=None, default_timeout=300, key_prefix=None):
        BaseCache.__init__(self, default_timeout)
        if servers is None or isinstance(servers, (list, tuple)):
            if servers is None:
                servers = ['127.0.0.1:11211']
            self._client = self.import_preferred_memcache_lib(servers)
            if self._client is None:
                raise RuntimeError('no memcache module found')
        else:
            # NOTE: servers is actually an already initialized memcache
            # client.
            self._client = servers

        self.key_prefix = to_bytes(key_prefix)
开发者ID:ArslanRafique,项目名称:werkzeug,代码行数:14,代码来源:cache.py


示例12: process_sql_result

def process_sql_result(rows):
    result = []
    for row in rows:
        fields = []
        for field in row:
            if PY2:
                if type(field) == buffer:
                    field = str(field)
            else:
                if type(field) == memoryview:
                    field = to_unicode(to_bytes(field, 'utf-8'), 'utf-8')
            fields.append(field)
        result.append(fields)
    return result
开发者ID:jam-py,项目名称:jam-py,代码行数:14,代码来源:postgres.py


示例13: make_cache_key

            def make_cache_key(*args, **kwargs):
                if callable(key_prefix):
                    cache_key = key_prefix()
                elif "%s" in key_prefix:
                    # 这里要转换成str(UTF-8)类型, 否则会报类型错误
                    _path = to_bytes(request.path, "utf-8")
                    # 对于非ASCII的URL,需要进行URL编码
                    if quote(_path).count("%25") <= 0:
                        _path = quote(_path)
                    cache_key = key_prefix % _path
                else:
                    cache_key = key_prefix

                return cache_key
开发者ID:vhaoyang,项目名称:wtxlog,代码行数:14,代码来源:ext.py


示例14: make_chunk_iter

def make_chunk_iter(stream, separator, limit=None, buffer_size=10 * 1024):
    """Works like :func:`make_line_iter` but accepts a separator
    which divides chunks.  If you want newline based processing
    you should use :func:`make_line_iter` instead as it
    supports arbitrary newline markers.

    .. versionadded:: 0.8

    .. versionadded:: 0.9
       added support for iterators as input stream.

    :param stream: the stream or iterate to iterate over.
    :param separator: the separator that divides chunks.
    :param limit: the limit in bytes for the stream.  (Usually
                  content length.  Not necessary if the `stream`
                  is otherwise already limited).
    :param buffer_size: The optional buffer size.
    """
    _iter = _make_chunk_iter(stream, limit, buffer_size)

    first_item = next(_iter, '')
    if not first_item:
        return

    _iter = chain((first_item,), _iter)
    if isinstance(first_item, text_type):
        separator = to_unicode(separator)
        _split = re.compile(r'(%s)' % re.escape(separator)).split
        _join = u''.join
    else:
        separator = to_bytes(separator)
        _split = re.compile(b'(' + re.escape(separator) + b')').split
        _join = b''.join

    buffer = []
    while 1:
        new_data = next(_iter, '')
        if not new_data:
            break
        chunks = _split(new_data)
        new_buf = []
        for item in chain(buffer, chunks):
            if item == separator:
                yield _join(new_buf)
                new_buf = []
            else:
                new_buf.append(item)
        buffer = new_buf
    if buffer:
        yield _join(buffer)
开发者ID:0x00xw,项目名称:wooyun,代码行数:50,代码来源:wsgi.py


示例15: load_interface

def load_interface(item):
    item._view_list = []
    item._edit_list = []
    item._order_list = []
    item._reports_list = []
    value = item.f_info.value
    if value:
        if len(value) >= 4 and value[0:4] == 'json':
            lists = json.loads(value[4:])
        else:
            lists = pickle.loads(to_bytes(value, 'utf-8'))
        item._view_list = lists['view']
        item._edit_list = lists['edit']
        item._order_list = lists['order']
        if lists.get('reports'):
            item._reports_list = lists['reports']
开发者ID:jam-py,项目名称:jam-py,代码行数:16,代码来源:common.py


示例16: __init__

    def __init__(self, data=None, secret_key=None, new=True):
        ModificationTrackingDict.__init__(self, data or ())
        # explicitly convert it into a bytestring because python 2.6
        # no longer performs an implicit string conversion on hmac
        if secret_key is not None:
            secret_key = to_bytes(secret_key, 'utf-8')
        self.secret_key = secret_key
        self.new = new

        if self.serialization_method is pickle:
            warnings.warn(
                "The default 'SecureCookie.serialization_method' will"
                " change from pickle to json in version 1.0. To upgrade"
                " existing tokens, override 'unquote' to try pickle if"
                " json fails.",
                stacklevel=2,
            )
开发者ID:gaoussoucamara,项目名称:simens-cerpad,代码行数:17,代码来源:securecookie.py


示例17: decode_netloc

    def decode_netloc(self):
        """Decodes the netloc part into a string."""
        rv = self.host or ''
        try:
            rv = to_bytes(rv, 'utf-8').decode('idna')
        except (AttributeError, TypeError, UnicodeError):
            pass

        if ':' in rv:
            rv = '[%s]' % rv
        port = self.port
        if port is not None:
            rv = '%s:%d' % (rv, port)
        auth = ':'.join(filter(None, [
            _url_unquote_legacy(self.raw_username or '', '/:%@'),
            _url_unquote_legacy(self.raw_password or '', '/:%@'),
        ]))
        if auth:
            rv = '%[email protected]%s' % (auth, rv)
        return rv
开发者ID:methane,项目名称:werkzeug,代码行数:20,代码来源:urls.py


示例18: tag

def tag(template, name, page=1):
    """
    :param template:
        模板文件,此参数自动传入
    :param name:
        Tag名称,若为非ASCII字符,一般是经过URL编码的
    """
    # 若name为非ASCII字符,传入时一般是经过URL编码的
    # 若name为URL编码,则需要解码为Unicode
    # URL编码判断方法:若已为URL编码, 再次编码会在每个码之前出现`%25`
    _name = to_bytes(name, "utf-8")
    if urllib.quote(_name).count("%25") > 0:
        name = urllib.unquote(_name)

    tag = Tag.query.filter_by(name=name).first_or_404()

    _url = page_url
    _query = Article.query.public().filter(Article.tags.any(id=tag.id))
    pagination = Page(_query, page=page, items_per_page=Article.PER_PAGE, url=_url)

    articles = pagination.items

    _template = template % (tag.template or "tag.html")
    return render_template(_template, tag=tag, pagination=pagination, articles=articles)
开发者ID:lehman3087,项目名称:toutiaoList2,代码行数:24,代码来源:views.py


示例19: test_dispatchermiddleware

def test_dispatchermiddleware():
    def null_application(environ, start_response):
        start_response("404 NOT FOUND", [("Content-Type", "text/plain")])
        yield b"NOT FOUND"

    def dummy_application(environ, start_response):
        start_response("200 OK", [("Content-Type", "text/plain")])
        yield to_bytes(environ["SCRIPT_NAME"])

    app = wsgi.DispatcherMiddleware(null_application, {"/test1": dummy_application, "/test2/very": dummy_application})
    tests = {
        "/test1": ("/test1", "/test1/asfd", "/test1/very"),
        "/test2/very": ("/test2/very", "/test2/very/long/path/after/script/name"),
    }
    for name, urls in tests.items():
        for p in urls:
            environ = create_environ(p)
            app_iter, status, headers = run_wsgi_app(app, environ)
            assert status == "200 OK"
            assert b"".join(app_iter).strip() == to_bytes(name)

    app_iter, status, headers = run_wsgi_app(app, create_environ("/missing"))
    assert status == "404 NOT FOUND"
    assert b"".join(app_iter).strip() == b"NOT FOUND"
开发者ID:auready,项目名称:werkzeug,代码行数:24,代码来源:test_wsgi.py


示例20: dump_cookie

def dump_cookie(key, value='', max_age=None, expires=None, path='/',
                domain=None, secure=False, httponly=False,
                charset='utf-8', sync_expires=True):
    """Creates a new Set-Cookie header without the ``Set-Cookie`` prefix
    The parameters are the same as in the cookie Morsel object in the
    Python standard library but it accepts unicode data, too.

    On Python 3 the return value of this function will be a unicode
    string, on Python 2 it will be a native string.  In both cases the
    return value is usually restricted to ascii as the vast majority of
    values are properly escaped, but that is no guarantee.  If a unicode
    string is returned it's tunneled through latin1 as required by
    PEP 3333.

    The return value is not ASCII safe if the key contains unicode
    characters.  This is technically against the specification but
    happens in the wild.  It's strongly recommended to not use
    non-ASCII values for the keys.

    :param max_age: should be a number of seconds, or `None` (default) if
                    the cookie should last only as long as the client's
                    browser session.  Additionally `timedelta` objects
                    are accepted, too.
    :param expires: should be a `datetime` object or unix timestamp.
    :param path: limits the cookie to a given path, per default it will
                 span the whole domain.
    :param domain: Use this if you want to set a cross-domain cookie. For
                   example, ``domain=".example.com"`` will set a cookie
                   that is readable by the domain ``www.example.com``,
                   ``foo.example.com`` etc. Otherwise, a cookie will only
                   be readable by the domain that set it.
    :param secure: The cookie will only be available via HTTPS
    :param httponly: disallow JavaScript to access the cookie.  This is an
                     extension to the cookie standard and probably not
                     supported by all browsers.
    :param charset: the encoding for unicode values.
    :param sync_expires: automatically set expires if max_age is defined
                         but expires not.
    """
    key = to_bytes(key, charset)
    value = to_bytes(value, charset)

    if path is not None:
        path = iri_to_uri(path, charset)
    domain = _make_cookie_domain(domain)
    if isinstance(max_age, timedelta):
        max_age = (max_age.days * 60 * 60 * 24) + max_age.seconds
    if expires is not None:
        if not isinstance(expires, string_types):
            expires = cookie_date(expires)
    elif max_age is not None and sync_expires:
        expires = to_bytes(cookie_date(time() + max_age))

    buf = [key + b'=' + _cookie_quote(value)]

    # XXX: In theory all of these parameters that are not marked with `None`
    # should be quoted.  Because stdlib did not quote it before I did not
    # want to introduce quoting there now.
    for k, v, q in ((b'Domain', domain, True),
                    (b'Expires', expires, False,),
                    (b'Max-Age', max_age, False),
                    (b'Secure', secure, None),
                    (b'HttpOnly', httponly, None),
                    (b'Path', path, False)):
        if q is None:
            if v:
                buf.append(k)
            continue

        if v is None:
            continue

        tmp = bytearray(k)
        if not isinstance(v, (bytes, bytearray)):
            v = to_bytes(text_type(v), charset)
        if q:
            v = _cookie_quote(v)
        tmp += b'=' + v
        buf.append(bytes(tmp))

    # The return value will be an incorrectly encoded latin1 header on
    # Python 3 for consistency with the headers object and a bytestring
    # on Python 2 because that's how the API makes more sense.
    rv = b'; '.join(buf)
    if not PY2:
        rv = rv.decode('latin1')
    return rv
开发者ID:211sandiego,项目名称:calllog211,代码行数:87,代码来源:http.py



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Python _compat.to_native函数代码示例发布时间:2022-05-26
下一篇:
Python _compat.text_type函数代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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