Commit 940e2ce7 authored by wanglei's avatar wanglei

...

parent 3c81d21e
import json import json
import os import os
import random
import re import re
import shutil import shutil
from base64 import b64encode, b64decode
import cv2 import cv2
import numpy as np import numpy as np
import regex as re2 import regex as re2
from Cryptodome.Cipher import AES
from Cryptodome.Random import get_random_bytes
import string import string
import random import random
...@@ -461,7 +455,9 @@ def get_random_string(length=8, is_ascii=True): ...@@ -461,7 +455,9 @@ def get_random_string(length=8, is_ascii=True):
global used_words # 使用全局变量 used_words global used_words # 使用全局变量 used_words
words = load_words_from_file('word_file.json') words = load_words_from_file('word_file.json')
if words: if words:
while words: max_attempts = 500 # 限制循环次数
attempts = 0
while words and attempts < max_attempts:
random_word = random.choice(words) random_word = random.choice(words)
if random_word not in used_words: if random_word not in used_words:
used_words.add(random_word) used_words.add(random_word)
...@@ -469,8 +465,12 @@ def get_random_string(length=8, is_ascii=True): ...@@ -469,8 +465,12 @@ def get_random_string(length=8, is_ascii=True):
return random_word return random_word
elif not is_ascii: elif not is_ascii:
return random_word return random_word
return None attempts += 1
# 如果单词列表为空或循环次数达到上限,则自动生成随机字符串
first_char = random.choice(string.ascii_lowercase)
rest_chars = ''.join(random.choice(string.ascii_lowercase) for _ in range(length - 1))
return first_char + rest_chars
def load_words_from_file(file_path): def load_words_from_file(file_path):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment