实验题目
认证审计系统
1、 设计一个信息系统,系统必须通过客户端录入账号口令远程登录;
2、 系统内至少包含三个以上账号;
3、 某账号登录后服务器端可实时显示该账号登录的时间及 IP 信息;
4、 服务器端可查询账号的历史登录信息。
解题思路
实现代码
服务端:
"""
认证审计系统
1、 设计一个信息系统,系统必须通过客户端录入账号口令远程登录;
2、 系统内至少包含三个以上账号;
3、 某账号登录后服务器端可实时显示该账号登录的时间及 IP 信息;
4、 服务器端可查询账号的历史登录信息。
"""
# 导入 socket datetime 模块
import socket, datetime
def check(user, passwd):
with open('账号密码.txt', 'r') as f:
for info in f.readlines():
admin = info.split(' ')[0]
key = info.split(' ')[1].rstrip('\n')
if user == admin:
if passwd == key:
return 'yes'
else:
return 'no'
return '!!!'
def login_info(user, addr):
user = str(user.decode())
with open('%s.txt' % user, 'a+') as f:
f.write('登陆时间:' + str(datetime.datetime.now()) + '--登陆IP:' + str(addr) + '\n')
return 0
def hostiry_info():
choice = input('是否需要查询历史账号信息(y/n):'.center(50))
if choice == 'y':
name = input('请输入你要查寻的账号名:')
with open('%s.txt' %name, 'w+') as f:
print(f.read())
return 0
def cishu(T, cli, hint):
T -= 1
if T:
msg = '登陆失败,' + hint + ',请重新登陆\n' + '您还剩%d次机会' % T
cli.send(msg.encode())
return T
else:
cli.send('exit'.encode())
return T
serversocket = socket.socket() # 创建 socket 对象
serversocket.bind(('0.0.0.0', 364)) # 绑定端口号
serversocket.listen(5) # 设置最大连接数,超过后排队
def server():
while True:
T = 3
cli, addr = serversocket.accept() # 建立客户端连接
print('连接已建立'.center(50))
msg = '请问是否要注册新帐号?(是请输入y):\n'
cli.send(msg.encode())
rec = cli.recv(1024)
if rec.decode() == 'True':
x = 1
while x:
x = 0
msg = '请输入你要注册的用户名:\n'
cli.send(msg.encode())
user = cli.recv(1024)
with open('账号密码.txt', 'r') as f:
for info in f.readlines():
new = info.split(' ')[0]
if user.decode() == new:
msg = '该帐号已存在,请重新输入新帐号'
cli.send(msg.encode())
x = 1
msg = '请输入你的密码:'
cli.send(msg.encode())
cipher = cli.recv(1024)
f = open('账号密码.txt', 'a')
f.write(user.decode() + ' ' + cipher.decode() + '\n')
open(user.decode()+'.txt', 'w')
f.close()
msg = '欢迎进入登录界面请输入账号密码:'
cli.send(msg.encode())
while True:
user = cli.recv(1024)
passwd = cli.recv(1024)
num = check(user.decode(), passwd.decode()) # 验证登录是否成功
cli.send(str(num).encode())
if num == 'yes': # 实时显示该账号登录的时间及 IP 信息
print('登录账号:' + str(user.decode()) + '--登陆时间:' + str(datetime.datetime.now()) + '--登陆IP:' + str(addr))
login_info(user, addr)
# 客户端登录成功,进入信息交互模块
while True:
data = cli.recv(1024).decode()
if data != 'exit':
print('客户发来:' + str(data))
else:
print('结束交互'.center(50))
break
break
else:
print(num)
if num == 'no':
hint = '密码错误'
if num == '!!!':
hint = '用户名错误'
T = cishu(T, cli, hint)
if not T:
break
hostiry_info()
cli.close()
if __name__ == '__main__':
server()
hostiry_info()
客户端:
# 加入socket 模块
import socket
clisocket = socket.socket() # 创建 socket 对象
host = socket.gethostbyname(socket.getfqdn(socket.gethostname()))
clisocket.connect((host, 364)) # 绑定端口号
msg = clisocket.recv(1024) # 保证已建立连接
image = input(msg.decode()) # 53行
if image == 'y':
image = 'True'
clisocket.send(image.encode()) # 54行
while True:
msg = clisocket.recv(1024)
image = input(msg.decode())
clisocket.send(image.encode()) # 59行
msg = clisocket.recv(1024)
print(msg.decode())
if msg.decode() == '请输入你的密码:':
break
msg = input()
clisocket.send(msg.encode())
else:
clisocket.send(image.encode()) #54行
msg = clisocket.recv(1024) # 保证已建立连接
print(msg.decode().center(50))
while True: # 可登陆三次
user = input('请输入用户名:')
clisocket.send(user.encode())
password = input('请输入密码:')
clisocket.send(password.encode())
# 判断是否需要重新登录
judge_info = clisocket.recv(1024).decode()
if judge_info == 'yes':
print('登录成功!!!')
# 进入交互模式,可实现多次消息传递
flag = 1
while flag == 1:
data = input('Please input message:')
if data != 'exit':
clisocket.send(data.encode())
else:
print('结束交互'.center(50))
clisocket.send(data.encode())
flag = 0
break
else:
msg = clisocket.recv(1024)
if msg != b'exit':
print(msg.decode())
else:
print('您的登陆机会已用完')
break
clisocket.close()