python密碼生成器的3種方法

2021-10-09 22:11:16 字數 1939 閱讀 2417

# -*- coding: utf-8 -*-

''' 32-47:空格 ! " # $ % & ' ( ) * + , - . /

48-57:0-9

58-64:: ; < = > ? @

65-90:a-z

91-96:[ 反斜槓 ] ^ _ `

97-122:a-z

123-126: ~

'''import itertools

import random

from scipy.special import comb, perm

這個方法比較方便快捷,但是比較消耗記憶體,記憶體不大,會記憶體溢位

def

products

(length=5)

: chars=

''.join(

[chr

(i)for i in

range(32

,127)]

)for i in itertools.product(chars, repeat = length)

: string =

''.join(i)

print

(string)

這個方法執行速度快,但是容易遺漏

'''

'''def

random_str

(code_len=5)

: chars=

''.join(

[chr

(i)for i in

range(32

,127)]

) code_count=

int(comb(

len(chars)

,code_len)

) count=

0while count < code_count:

checkcode=

''for i in

range

(code_len)

: j=random.randint(0,

len(chars)-1

) checkcode+= chars[j]

print

(checkcode)

count+=

1

這個方法綜合前兩種,記憶體消耗不大,執行速度一般

def

sub_list

(length=5)

: chars =

[chr

(j)for i in

range

(length)

for j in

range(32

,127)]

for i in

range(1

<<

len(chars)):

combo_list =

for j in

range

(len

(chars)):

if i &(1

<< j):)

sub_list_len =

len(combo_list)

if sub_list_len != length:

continue

else

: sub_str =

''.join(combo_list)

print

(sub_str)

defmain()

: products(

) random_str(

) sub_list(

)if __name__ ==

'__main__'

: main(

)

python 密碼生成器

乙個密碼生成器,可以生成規定位數的密碼,也可以全部列舉出來 import random import string import itertools 隨機生成num位數的密碼,密碼裡面包含a z,a z,0 9 def getrandomnumkey num a string.ascii lette...

Python密碼生成器

密碼生成器 密碼格式為 密碼 空格 字元 隨機產生自定義長度 10.1.1.1 v z 10.1.1.3 prrqiku ocj 10.1.1.4 sx z.file open self.fname iplist,r for ip in file.readlines file.close print...

python3生成器 Python3 生成器

python3 生成器 閱讀 125 發布於 2020 05 19 14 29 25 在python中,一邊迴圈一邊計算出元素的機制,稱為生成器 generator。生成器的優點 一次返回乙個結果,延遲計算。這對於大資料量處理,是個非常有用的優勢。占用記憶體量是工程師必須考慮的乙個問題。提高 可讀性...