Django包生成随机字母数字字符串 [英] Django package to generate random alphanumeric string

查看:310
本文介绍了Django包生成随机字母数字字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个具有分离前端(Angular或其他JS库)和后端(Django)的应用程序。为了确保发送到服务器的请求的一些安全性,我想附加一个url参数,说 server / someurl?unique_id = Something-unique



我正在特定时间将这个唯一的代码存储在机器的 localStorage 上。但是,我想使用服务器端的某种功能来设置此代码,这不仅会生成这种随机字母数字文本,而且还会根据传入的请求进行验证。



例如:



当用户打开应用程序时,它会发送一个 server / setCode 这将使用这个随机生成的字符串进行响应,我将使用JS存储到本地存储



在外发请求中,请说服务器可以验证的 server / getdata?someparameter = some_data& unique_id = string_from_local_storage 反对生成函数,然后处理其余的url。



有没有一个软件包或模块可以帮助我实现生成和验证?
我希望能够传达我想要的东西,因为我无法找到任何解决方案来编写这个函数来生成和测试自己。

解决方案

Django提供函数 get_random_string(),这将满足字母数字字符串生成要求。您不需要任何额外的包,因为它位于 django.utils.crypto 模块中。

 >>>从django.utils.crypto import get_random_string 
>>> unique_id = get_random_string(length = 32)
>>> unique_id
u'rRXVe68NO7m3mHoBS488KdHaqQPD6Ofv'

您还可以使用 allowed_chars

 >>> short_genome = get_random_string(length = 32,allowed_chars ='ACTG')
>>> short_genome
u'CCCAAAAGTACGTCCGGCATTTGTCCACCCCT'

有许多其他方法可以生成唯一的ID,不一定是字母数字:


  1. uuid 模块 - 使用 uuid1 uuid4 生成唯一的UUID,例如

     >>> import uuid 
    >>> my_uuid = uuid.uuid4()
    >>> my_uuid
    UUID('8e6eee95-eae1-4fb4-a436-27f68dbcb6d7')
    >>> str(my_uuid)
    '8e6eee95-eae1-4fb4-a436-27f68dbcb6d7'


  2. 随机模块:

     >>>导入随机
    >>>>导入字符串
    >>> allowed_chars =''.join((string.lowercase,string.uppercase,string.digits))
    >>>对于_范围(32)中的_ unique_id =''.join(random.choice(allowed_chars))
    >>> unique_id
    '121CyaSHHzX8cqbgLnIg1C5qNrnv21uo'


或如果你对字母表不是很;::

 >>> unique_id ='%32x'%random.getrandbits(16 * 8)
>>> unique_id
'5133d2d79ce518113474d8e9f3702638'


I'm building an app that has a separated front-end (Angular or some other JS library) and backend (Django). To ensure some security of requests being sent to the server, I want to append a url parameter say server/someurl?unique_id=Something-unique.

I am storing this unique code on the machine's localStorage for a specific time. However, I want to set this code using some sort of function on the server end which will not only generate this random alphanumeric text but also validate it based on incoming requests.

For example:

When a user opens the app, it'll send a server/setCode which will respond with this randomly generated string which I will store to Local Storage using JS.

On an outgoing request, say server/getdata?someparameter=some_data&unique_id=string_from_local_storage which the server can validate against the generating function and only then process the rest of the url.

Is there a package or a module that could help me achieve the generation and validation? I hope I could convey what I want as I'm not able to find any solution for this short of writing the function to generate and test myself.

解决方案

Django provides the function get_random_string() which will satisfy the alphanumeric string generation requirement. You don't need any extra package because it's in the django.utils.crypto module.

>>> from django.utils.crypto import get_random_string
>>> unique_id = get_random_string(length=32)
>>> unique_id
u'rRXVe68NO7m3mHoBS488KdHaqQPD6Ofv'

You can also vary the set of characters with allowed_chars:

>>> short_genome = get_random_string(length=32, allowed_chars='ACTG')
>>> short_genome
u'CCCAAAAGTACGTCCGGCATTTGTCCACCCCT'

There are many other ways to generate a unique id, though not necessarily an alphanumeric one:

  1. The uuid module - generate a unique UUID using uuid1 or uuid4, e.g.

    >>> import uuid
    >>> my_uuid = uuid.uuid4()
    >>> my_uuid
    UUID('8e6eee95-eae1-4fb4-a436-27f68dbcb6d7')
    >>> str(my_uuid)
    '8e6eee95-eae1-4fb4-a436-27f68dbcb6d7'
    

  2. The random module:

    >>> import random
    >>> import string
    >>> allowed_chars = ''.join((string.lowercase, string.uppercase, string.digits))
    >>> unique_id = ''.join(random.choice(allowed_chars) for _ in range(32))
    >>> unique_id
    '121CyaSHHzX8cqbgLnIg1C5qNrnv21uo'
    

Or, if you're not fussy about the alphabet:

>>> unique_id = '%32x' % random.getrandbits(16*8)
>>> unique_id
'5133d2d79ce518113474d8e9f3702638'

这篇关于Django包生成随机字母数字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆