Python Django包生成随机字母数字字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25943850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django package to generate random alphanumeric string
提问by Newtt
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.
我正在构建一个具有独立前端(Angular 或其他一些 JS 库)和后端(Django)的应用程序。为了确保发送到服务器的请求的某些安全性,我想附加一个 url 参数 say server/someurl?unique_id=Something-unique。
I am storing this unique code on the machine's localStoragefor 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.
我将此唯一代码存储在机器上localStorage的特定时间。但是,我想在服务器端使用某种函数设置此代码,该函数不仅会生成此随机字母数字文本,还会根据传入请求对其进行验证。
For example:
例如:
When a user opens the app, it'll send a server/setCodewhich will respond with this randomly generated string which I will store to Local Storageusing JS.
当用户打开应用程序时,它会发送一个server/setCode响应随机生成的字符串,我将Local Storage使用 JS存储该字符串。
On an outgoing request, say server/getdata?someparameter=some_data&unique_id=string_from_local_storagewhich the server can validate against the generating function and only then process the rest of the url.
在传出请求中,说明server/getdata?someparameter=some_data&unique_id=string_from_local_storage服务器可以针对生成函数验证哪个,然后才处理 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.
有没有可以帮助我实现生成和验证的包或模块?我希望我能传达我想要的东西,因为我无法找到任何解决方案来编写生成和测试自己的函数。
采纳答案by mhawke
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.cryptomodule.
Django 提供了get_random_string()满足字母数字字符串生成要求的函数。您不需要任何额外的包,因为它在django.utils.crypto模块中。
>>> 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:
您还可以使用以下命令更改字符集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:
还有许多其他方法可以生成唯一 ID,但不一定是字母数字:
The uuidmodule - generate a unique UUID using
uuid1oruuid4, e.g.>>> import uuid >>> my_uuid = uuid.uuid4() >>> my_uuid UUID('8e6eee95-eae1-4fb4-a436-27f68dbcb6d7') >>> str(my_uuid) '8e6eee95-eae1-4fb4-a436-27f68dbcb6d7'The randommodule:
>>> import random >>> import string >>> allowed_chars = ''.join((string.ascii_letters, string.digits)) >>> unique_id = ''.join(random.choice(allowed_chars) for _ in range(32)) >>> unique_id '121CyaSHHzX8cqbgLnIg1C5qNrnv21uo'
该UUID模块-使用生成一个唯一的UUID
uuid1或者uuid4,如>>> import uuid >>> my_uuid = uuid.uuid4() >>> my_uuid UUID('8e6eee95-eae1-4fb4-a436-27f68dbcb6d7') >>> str(my_uuid) '8e6eee95-eae1-4fb4-a436-27f68dbcb6d7'在随机模块:
>>> import random >>> import string >>> allowed_chars = ''.join((string.ascii_letters, 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'

