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


Python _compat.to_bytes函数代码示例

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


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


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