def _random_string(length = None, allowed_chars = None):
'''
Generate a random string of the given length.
:param length: length of the string (defaults to settings.NONCE_LENGTH)
:rtype length: int
:param allowed_chars: characters to allow in string
:rtype allowed_chars: str
:return: generated string
:rtype: str
'''
if allowed_chars is None:
try:
allowed_chars = string.letters
except AttributeError:
allowed_chars = string.ascii_letters
if length is None:
length = settings.NONCE_LENGTH
s = ''.join(random.choice(allowed_chars) for _ in range(length))
return s
评论列表
文章目录