def get_classes_list(belong_to_course): data = rw.read(databases) logger.info("可选择的班级有:") for classes_name in data["classes"]: if data["classes"][classes_name][ "belong_to_course"] == belong_to_course: print(classes_name)
def get_teacher_list(belong_to_school): data = rw.read(databases) logger.info("可选择的讲师有:") for teacher_name in data["teacher"]: if data["teacher"][teacher_name][ "belong_to_school"] == belong_to_school: teacher_major = data["teacher"][teacher_name]["teacher_major"] print("名字", teacher_name, "专业:", teacher_major) # get_school_list() # get_course_list("撸码学院北京校区") # get_classes_list("python自动化开发") # get_teacher_list("撸码学院北京校区")
def server_done(self): while True: data = rw.read(user_file) user_status = False conn,addr = self.server.accept() print("连接来了.....") while True: if not user_status: user = conn.recv(102400).decode("utf-8") #接收客户端输入的用户名 #conn.sendall("get user".encode("utf-8")) #回应一下已收到用户名 passwd = conn.recv(102400).decode("utf-8") #接收客户端输入的密码 #conn.sendall("get passwd".encode("utf-8")) #回应一下已收到密码 print(user,passwd) user_dir = "./%s"%user if not os.path.exists(us
#!/usr/bin/env python # _*_ coding: utf-8 _*_ # author:chenjianwen # email:[email protected] import os import sys sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from lib.logger import logger from core.modules import Teacher from lib.init_db_dir import databases from lib import read_and_write_file as rw data = rw.read(databases) def begin_classes(): logger.info("===============设置班级上课===============") teacher_ID = input("请输入讲师ID:").strip() teacher_name = input("请输入讲师名字:").strip() logger.info("===============班级列表===============") for classes_name in data["classes"]: print(classes_name) classes_name = input("请输入需要上课的班级:").strip() th = Teacher(teacher_ID, teacher_name) th.classing_in(classes_name) def end_classes(): logger.info("===============设置班级下课===============") teacher_ID = input("请输入讲师ID:").strip() teacher_name = input("请输入讲师名字:").strip() logger.info("===============班级列表===============")
def get_course_list(belong_to_school): data = rw.read(databases) logger.info("可选择的课程有:") for course_name in data["course"]: if data["course"][course_name]["belong_to_school"] == belong_to_school: print(course_name)
def get_school_list(): data = rw.read(databases) logger.info("可选择的学校有:") for school_name in data["school"]: print(school_name)
#!/usr/bin/env python # _*_ coding: utf-8 _*_ # author:chenjianwen # email:[email protected] import json from lib import read_and_write_file as rw user_file = "./db/user.json" user_status = False data = rw.read(user_file) def login(user, passwd): def auth(func): def inner(*args, **kwargs): global user_status if not user_status: #user = input("name:").strip() #passwd = input("password:").strip() try: password = data[user]["passwd"] except KeyError as e: exit("用户名或密码错误...") if data[user] and passwd == password: print("[%s],welcom login ...." % user) user_status = True else: exit("用户名或密码错误...") if user_status: return func(*args, **kwargs)
def server_done(self): while True: data = rw.read(user_file) user_status = False m = hashlib.md5() conn, addr = self.server.accept() ##阻塞状态,等待连接进来... logger.info("连接来了.....") while True: if not user_status: user = conn.recv(1024).decode( "utf-8") #接收客户端输入的用户名 数据包大小:官方建议最大8192(8k) conn.send("get user".encode("utf-8")) #回应一下已收到用户名 passwd = conn.recv(1024).decode("utf-8") #接收客户端输入的密码 conn.send("get passwd".encode("utf-8")) #回应一下已收到密码 print(user, passwd) user_dir = os.path.join(os.getcwd(), "..", "user_home", "%s") % user #print(user_dir) if not os.path.exists(user_dir): #如果登陆用户的目录不存在,则创建 os.makedirs(user_dir) try: password = data[user]["passwd"] except KeyError as e: exit("用户名或密码错误...") if data[user] and passwd == password: #验证账号密码是否正确 logger.info("user:[%s],addr:[%s],login susess...." % (user, addr)) user_status = True if user_status: cmd = None filename = None data = None cmd = conn.recv(1024).decode("utf-8") #接收需要执行的命令 print(cmd) if not cmd: continue conn.sendall("ack server: get cmd".encode( "utf-8")) #回应客户端已收到需要执行的命令 if cmd == "q": logger.info("user:[%s],addr:[%s],连接断开....." % (user, addr)) break elif "get" in cmd: filename = conn.recv(1024).decode("utf-8") #接收文件名称 conn.sendall( "get filename".encode("utf-8")) #回应客户端已收到文件名称 conn.recv(1024) ##接收客户端说可以开始发送数据的指示 logger.info("user:[%s],addr:[%s],get [%s]" % (user, addr, filename)) file = "%s/%s" % (user_dir, filename) if os.path.isfile(file): file_size = str(os.stat(file).st_size).encode( "utf-8") ##获取文件大小,转换为二进制 conn.send(file_size) ##发送文件大小给客户端 f = open(file, "rb") for line in f: m.update(line) conn.send(line) #发送对应文件数据到客户端 file_md5 = m.hexdigest() #获取文件的MD5值 conn.send(file_md5.encode("utf-8")) #发送MD5值 logger.info("user:[%s],addr:[%s],下载文件[%s]成功" % (user, addr, filename)) f.close() else: file_size = str("0").encode( "utf-8") ##获取文件大小,转换为二进制 conn.send(file_size) ##发送文件大小给客户端 print("文件不存在") elif "push" in cmd: filename = conn.recv(1024).decode( "utf-8") #若果是上传,则接收第二条数据,接收文件数据 conn.send( "get filename".encode("utf-8")) #回应客户端已收到文件名称 logger.info("user:[%s],addr:[%s],push [%s]" % (user, addr, filename)) file_size = int( conn.recv(1024).decode("utf-8")) #接收上传的文件大小 conn.send("I get file_size".encode("utf-8")) recv_size = 0 f = open("%s/%s" % (user_dir, filename), 'wb') while recv_size < file_size: if file_size - recv_size > 1024: size = 1024 else: size = file_size - recv_size data = conn.recv(size) #接收客户端传过来的文件数据 m.update(data) recv_size += len(data) f.write(data) else: client_file_md5 = conn.recv(1024).decode( "utf-8") # 接收服务端发来的MD5 recv_file_md5 = m.hexdigest() if client_file_md5 == recv_file_md5: print("已校验MD5值,上传成功") conn.send("已校验MD5值,上传成功".encode("utf-8")) else: print("MD5值校验失败,文件数据有错,上传失败") conn.send( "MD5值校验失败,文件数据有错,上传失败".encode("utf-8")) f.close() elif "push" not in cmd and "get" not in cmd: conn.recv(1024) #接收客户端可以执行的信号 if cmd == "ls" or cmd == "dir": #判断执行那种命令 cmd_list = "%s %s" % (cmd, user_dir) print(cmd_list) msg = os.popen("%s %s" % (cmd, user_dir)).read() else: msg = os.popen(cmd).read() #print(msg) if msg: msg_size = str(len(msg)).encode("utf-8") ###获取结果长度 conn.send(msg_size) ##发送长度给客户端 conn.recv(1024) ##接收客户端的回应,他已收到长度了 conn.sendall(msg.encode("utf-8")) #发送执行结果内容给客户端 logger.info("user:[%s],addr:[%s],执行命令[%s]" % (user, addr, cmd)) else: msg = "执行命令错误" msg_size = str(len(msg)).encode("utf-8") ###获取结果长度 conn.send(msg_size) ##发送长度给客户端 conn.recv(1024) ##接收客户端的回应,他已收到长度了 conn.sendall(msg.encode("utf-8")) logger.info( "user:[%s],addr:[%s],执行命令出错或命令不存在,[%s]" % (user, addr, cmd)) continue else: logger.error("user:[%s],addr:[%s],不符合命令要求.....断开" % (user, addr)) break
def __init__(self, student_ID, student_name): self.student_ID = student_ID self.student_name = student_name self.data = rw.read(databases)
def __init__(self, teacher_ID, teacher_name): self.teacher_ID = teacher_ID self.teacher_name = teacher_name self.data = rw.read(databases)
def __init__(self, schoo_name, school_address, school_desc): self.schoo_name = schoo_name self.school_address = school_address self.school_desc = school_desc self.data = rw.read(databases)