1。四位数字字母验证码的生成实例
1 import random 2 if __name__ =="__main__": #四位数字字母验证码的生成 3 checkcode="" #保存验证码的变量 4 for i in range(4): 5 index=random.randrange(0,4) #生成一个0~3中的数 6 if index!=i and index +1 !=i: 7 checkcode +=chr(random.randint(97,122)) # 生成a~z中的一个小写字母 8 elif index +1==i: 9 checkcode +=chr(random.randint(65,90) ) # 生成A~Z中的一个大写字母 10 else: 11 checkcode +=str(random.randint(1,9)) # 数字1-9 12 print(checkcode)
输出为:m47A、8wQ9、vugS
-------------------------------
2。格式化时间函数
1 def formatTime(longtime): 2 '''格式化时间的函数''' 3 import time 4 return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(longtime))
--------------------------------
3。记录显示登录日志实例
import time def show_info(): print('''输入提示数字,执行相应操作 0:退出 1:查看登录日志 ''') def write_loginfo(username): """ 将用户名和登录时间写入日志 :param username: 用户名 """ with open('log.txt','a') as f: string = "用户名:{} 登录时间:{}\n".format(username ,time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) f.write(string) def read_loginfo(): """ 读取日志 """ with open('log.txt','r') as f: while True