password_handle.py 937 B

12345678910111213141516171819202122232425262728293031
  1. import asyncio
  2. import random
  3. import string
  4. async def generate_password(length=10):
  5. if length < 6: # 至少需要3位密码以包含所有必要的字符
  6. raise ValueError("Password length should be at least 3")
  7. # 确保密码中至少包含一个字母和一个数字
  8. password = [
  9. random.choice(string.ascii_uppercase), # 大写字母
  10. random.choice(string.ascii_lowercase), # 小写字母
  11. random.choice(string.digits) # 数字
  12. ]
  13. # 添加剩余的随机字符
  14. characters = string.ascii_letters + string.digits
  15. password.extend(random.choice(characters) for _ in range(length - 3))
  16. # 打乱密码以确保随机性
  17. random.shuffle(password)
  18. # 将列表转换为字符串
  19. return ''.join(password)
  20. if __name__ == "__main__":
  21. # 生成一个10位的密码
  22. asyncio.run(generate_password(10))
  23. # password = generate_password(10)
  24. # print(password)