import sys
import string
import secrets
alphabet = string.ascii_letters + string.digits
punctuation = string.punctuation
def random_passwd(length=18):
"""随机生成指定位数的密码"""
passwd_table = alphabet + punctuation
while True:
passwd = ''.join(secrets.choice(passwd_table) for i in range(length))
if (any(p.islower() for p in passwd) and \
any(p.isupper() for p in passwd) and \
any(p in passwd for p in punctuation) and \
sum(p.isdigit() for p in passwd) >= 3):
return passwd
if __name__ == '__main__':
length = int(sys.argv[1])
passwd = random_passwd(length)
print(f'\n{length}位随机密码: {passwd}')