1
這是一個愚蠢的問題......老實說,我真的沒有在目前的線索和我相當新手蟒蛇...蟒蛇 - 隨機字符串發生器_help_
我目前衝倒我的頭一個生成隨機密碼的python腳本。我發現了一個良好的開端從here例如通過「奧米德拉哈」
編輯:上revist,這個例子是遠爲複雜,似乎有執行相同的任務要簡單得多的方式東西...
import random
import hashlib
import time
"""
This script is adapted from an example found here:https://stackoverflow.com/questions/18319101/whats-the-best-way-to-generate-random-strings-of-a-specific-length-in-python ; originally provided by user 'Omid Raha'
"""
SECRET_KEY = 'ffdsat9asdf5o5u9HKHvurtiasdf1tg1V36jyasdfSv8Ppin9O'
try:
random = random.SystemRandom()
using_sysrandom = True
except NotImplementedError:
import warnings
warnings.warn('A secure pseudo-random number generator is not available '
'on your system. Falling back to Mersenne Twister.')
using_sysrandom = False
def get_random_string(length=12,
allowed_chars='abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'%*/[email protected]'):
"""
Returns a securely generated random string.
The default length of 12 with the a-z, A-Z, 0-9 character set returns
a 71-bit value. log_2((26+26+10)^12) =~ 71 bits
"""
if not using_sysrandom:
# This is ugly, and a hack, but it makes things better than
# the alternative of predictability. This re-seeds the PRNG
# using a value that is hard for an attacker to predict, every
# time a random string is required. This may change the
# properties of the chosen random sequence slightly, but this
# is better than absolute predictability.
random.seed(
hashlib.sha256(
("%s%s%s" % (
random.getstate(),
time.time(),
SECRET_KEY)).encode('utf-8')
).digest())
return ''.join(random.choice(allowed_chars) for i in range(length))
print (get_random_string)
簡單的返回:
<function get_random_string at 0x1034f7848>
我不知道這意味着什麼......或者如果我前夕n正確執行腳本。
編輯:
謝謝你,閱讀
明明白白地說,你要打印的函數的定義,當你想調用的函數,並打印值。嘗試'print(get_random_string())' - 函數名稱信號後面的括號,以便實際運行該函數,並獲取其生成的值。 – Monkpit
@monkey謝謝:)這對未來的工作非常有幫助 – knope