alphabet = string.ascii_letters + string.digits
punctuation = string.punctuation
def random_passwd(length=18):
passwd_table = alphabet + punctuation
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):
if __name__ == '__main__':
length = int(sys.argv[1])
passwd = random_passwd(length)
print(f'\n{length}位随机密码: {passwd}')