用Python生成API KEY和SECRET的最简单,最安全的方法是什么 - python

我需要生成将存储在Redis服务器中的API密钥和密钥。生成密钥和机密的最佳方法是什么?

我正在开发一个基于Django-tastypie框架的应用程序。

python大神给出的解决方案

编辑:对于生成随机数的一种非常安全的方式,您应该使用urandom:

from binascii import hexlify

key = hexlify(os.urandom(length))

这将产生字节,如果需要字符串,请调用key.decode()

您可以通过python方式生成所需长度的键:

import random
import string

def generate_key(length):
    return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length))

然后,您可以使用所需的长度key = generate_key(40)来调用它。
您可以指定要使用的字母,例如仅对包含小写字母等的键使用string.ascii_lowercase

好吃的中还有Api认证模型,可能值得一试https://django-tastypie.readthedocs.org/en/latest/authentication.html#apikeyauthentication

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

如何打印浮点数的全精度[Python] - python

我编写了以下函数,其中传递了x,y的值:def check(x, y): print(type(x)) print(type(y)) print(x) print(y) if x == y: print "Yes" 现在当我打电话check(1.00000000000000001, 1.0000000000000002)它正在打印:<…

PyCharm中Django的文档字符串中未解决的引用 - python

我在Django的专案中使用Google Style Python Docstrings like in this Example。当我创建一个类并在文档字符串中使用属性符号时,Pycharm总是说-“未解决的引用”。class Post(models.Model): """ Class for posts. Attribute…