需求:
- 根据列表, 选择要登录的页面;
- 根据不同的页面, 选择不同的登录账号, 账号多个, 可以循环;
- 登录以后的状态可以保留, 在退出后变为False, 下次再次登录;
我们这里用了jingdong和weixin两批账号, 每批账号可以有多个, 写在文件中; 根据不同的选择, 显示不同的页面: Home, Fiance, Book.
# _Author: lWX548594
# _Date: 2018/7/23 0023
# _ScriptName: login
def read_jingdong(): # 定义读取jingdong账号的函数
global line
line = {}
f = open("jingdong", 'r+')
for lines in f:
key, value = lines.strip().split()
line[key] = value
def read_weixin(): # 定义读取微信账号的函数
global line
line = {}
f = open("weixin", 'r+')
for lines in f:
key, value = lines.strip().split()
line[key] = value
def init(): # 初始化函数
pages = ['Home', 'Fiance', 'Book']
for i in enumerate(pages, 1):
print(i)
choice = input("Please input your choice, q for quit: ")
while True:
if choice == 'q':
print("Exit..........")
file3 = open('auth_file', 'w')
file3.write('False') # q退出, 将auth_file文件写入False, 注意, 这是字符串格式, 不是bool格式.
exit()
elif not choice.isdigit():
print("Please input a number.")
choice = input("Please input your choice: ")
elif int(choice) not in range(1, 4):
print("Out of range...")
exit()
else:
break
global page_choice # 函数中的变量, 通常需要声明一下全局, 否则其他函数调用不了, 目前学的少, 以后可能有更好的方法.
if choice == '1':
page_choice = "Home"
read_jingdong() # 根据不同的页面, 选择读入不同的账号
elif choice == '2':
page_choice = "Fiance"
read_weixin()
elif choice == '3':
page_choice = "Book"
read_jingdong()
else:
pass
auth_file = open('auth_file', 'r')
global auth_type
auth_type = auth_file.readline()
print("auth_type: %s" % auth_type)
def which_flag(flag): # 装饰器, 传入一个flag, 来确定是否登录
def login(f):
def inner(a_choice):
print("Flag: %s" % flag)
if flag == 'False': # 字符串格式, 不是bool...
print("You have to login first...")
while True:
my_username = input("Please input your username: ")
my_pass = input("Please input your password: ")
for i in line.keys(): # 循环验证用户
break_flag = False
if i == my_username:
if line[i] == my_pass:
print("Login successfully...")
file2 = open('auth_file', 'w')
file2.write('True') # 成功后将auth_file写入True
break_flag = True
break
else:
print("Wrong username or password. Please try again.") # 所有账户验证失败, 显示提示信息
if break_flag:
break
f(a_choice)
return inner
return login
while True: # 循环开始进行页面显示
init()
@which_flag(auth_type)
def show_page(my_choice):
print("This is %s page..." % my_choice)
show_page(page_choice)
其中, weixin和jingdong两个账号密码文件格式如下:
lius 7788
lius1 7788
lius2 7788
auth_file里面事先写入False:
False
脚本运行结果展示:
C:\Users\lwx548594\PycharmProjects\LiusProject\venv\Scripts\python.exe C:/Users/lwx548594/PycharmProjects/LiusProject/Day17/login.py
(1, 'Home')
(2, 'Fiance')
(3, 'Book')
Please input your choice, q for quit: 1
auth_type: False
Flag: False
You have to login first...
Please input your username: lius
Please input your password: 7788
Login successfully...
This is Home page...
(1, 'Home')
(2, 'Fiance')
(3, 'Book')
Please input your choice, q for quit: 3
auth_type: True
Flag: True
This is Book page...
(1, 'Home')
(2, 'Fiance')
(3, 'Book')
Please input your choice, q for quit: 2
auth_type: True
Flag: True
This is Fiance page...
(1, 'Home')
(2, 'Fiance')
(3, 'Book')
Please input your choice, q for quit: q
Exit..........
Process finished with exit code 0
以上脚本是自己瞎写的, 有不对的地方, 请大家指正.
发表回复
要发表评论,您必须先登录。