思路:
1.生成全字符的单密码有94个,两个密码有94*94,三个密码有94*94*94个
2.连接到oracle数据库,将生成的密码字典插入oracle表
3 验证字典是否成功插入
第一步
def threefor(): data=[] for key1 in word: for key2 in word: for key3 in word: data.append({‘wordkey‘: key1 + key2 + key3}) return data
第二步连接到数据库:这个是db_config_dba
import cx_Oracle username="" password="" host="" port= instance="" tns = cx_Oracle.makedsn(host,port,instance) con=cx_Oracle.connect(username,password,tns, mode=cx_Oracle.SYSDBA, encoding="UTF-8" ,nencoding="UTF-8")
第三步插入表,以下是全部python 语句
import datetime import gc import db_config_dba #步骤二的python文件 import time def threefor(): data=[] for key1 in word: for key2 in word: for key3 in word: data.append({‘wordkey‘: key1 + key2 + key3}) return data if __name__ == "__main__": lowercase = ‘abcdefghijklmnopqrstuvwxyz‘ uppercase = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘ digits = ‘0123456789‘ special = """!"#$%&‘( )*+,-./:;<=>?@[]^_`{|}~""" word = lowercase + uppercase + digits + special gc.collect() print(‘\n‘) print(f‘word content is: {word}‘) print(f‘Total number is: {len(word)}‘) print(‘\n‘) starttime = datetime.datetime.now() print(‘Start generate password: ‘) print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))) #create oracle db connection DB = db_config_dba.con cursor = DB.cursor() #delete librarykey in target oracle db database sql=‘drop table librarykey purge‘ cursor.execute(sql) #create librarykey table in target db database sql=‘create table librarykey ( wordkey varchar(20),CONSTRAINT work_key primary key (wordkey))‘ cursor.execute(sql) #generate insert sql for table librarykey sql = ‘insert into librarykey (wordkey) values (:wordkey)‘ cursor.executemany(sql, threefor()) DB.commit() #查询数据库的记录数 sql = ‘select count(*) from librarykey‘ cursor.execute(sql) for row in cursor: print(row[0]) endtime = datetime.datetime.now() print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))) print(f‘The time cost: {endtime - starttime}‘) print(‘\n‘) #verfication the key number‘s as the sql result is tuple, so we should use row[0] if row[0] == len(word)*len(word)*len(word): print(f‘total password number should be : {len(word)*len(word)*len(word)}‘) else: print("not all the password is generate,pls try again") cursor.close() DB.close() gc.collect()
结果:
word content is: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&‘( )*+,-./:;<=>?@[]^_`{|}~ Total number is: 94 Start generate password: 2020-05-29 23:10:28 830584 2020-05-29 23:10:36 The time cost: 0:00:07.987566 total password number should be : 830584 Process finished with exit code 0
原文:https://www.cnblogs.com/tingxin/p/12990097.html