def user_reg(): ''' 用户注册 ''' uname = input("请输入用户名:") passwd = input("请输入密码:") phone = input("请输入手机号:") email = input("请输入邮箱:") if not user_reg_login.user_reg(uname, passwd, phone, email): print("注册失败") else: print("用户%s注册成功!" % uname)
def user_reg(): ''' 函数内容:用户注册 ''' user_name = input("请输入用户名:") user_passwd = input("请输入密码:") user_phone = input("请输入手机号:") user_email = input("请输入邮箱:") if not user_reg_login.user_reg(user_name, user_passwd, user_phone, user_email): print("注册失败!") else: print("用户%s注册成功!" % user_name)
def user_service_thread(sock_conn, client_addr): try: data_len = sock_conn.recv(15).decode().rstrip() if len(data_len) > 0: data_len = int(data_len) recv_size = 0 json_data = b"" while recv_size < data_len: tmp = sock_conn.recv(data_len - recv_size) if tmp == 0: break json_data += tmp recv_size += len(tmp) json_data = json_data.decode() req = json.loads(json_data) if req["op"] == 1: # 登录校验 rsp = {"op": 1, "error_code": 0} if user_reg_login.check_uname_pwd(req["args"]["uname"], req["args"]["passwd"]): rsp["error_code"] = 1 header_data = json.dumps(rsp).encode() data_len = "{:<15}".format(len(header_data)).encode() sock_conn.send(data_len) sock_conn.send(header_data) if not rsp["error_code"]: print("用户%s登录成功,开始传输文件......" % req["args"]["uname"]) if os.path.isdir(dest_file_abs_path): send_dir(sock_conn) else: send_one_file(sock_conn, dest_file_abs_path) elif req["op"] == 2: # 用户注册 rsp = {"op": 2, "error_code": 0} if not user_reg_login.user_reg( req["args"]["uname"], req["args"]["passwd"], req["args"]["phone"], req["args"]["email"]): # 注册失败 rsp["error_code"] = 1 else: print("用户%s注册成功!" % req["args"]["uname"]) rsp = json.dumps(rsp).encode() data_len = "{:<15}".format(len(rsp)).encode() sock_conn.send(data_len) sock_conn.send(rsp) elif req["op"] == 3: # 校验用户名是否存在 rsp = {"op": 3, "error_code": 0} ret = user_reg_login.check_user_name(req["args"]["uname"]) if ret == 2: rsp["error_code"] = 1 print("校验用户名%s失败!" % req["args"]["uname"]) else: print("校验用户名%s成功!" % req["args"]["uname"]) rsp = json.dumps(rsp).encode() data_len = "{:<15}".format(len(rsp)).encode() sock_conn.send(data_len) sock_conn.send(rsp) finally: print("客户端(%s:%s)断开连接!" % client_addr) sock_conn.close()
def user_service_thread(sock_conn): # 将接收的消息转换为字符型,且删除右边的的空白符合 data_len = sock_conn.recv(15).decode().rstrip() print(data_len) if len(data_len) > 0: data_len = int(data_len) # 接收变量 recv_size = 0 json_data = "" while recv_size < data_len: tmp = sock_conn.recv(data_len - recv_size).decode() if len(tmp) == 0: break json_data += tmp recv_size += len(tmp) print(json_data) # 此处有修改 req = json.loads(json_data) # 将字符串转换为字典 # 登录校验 if req["op"] == 1: # 登录校验 rsp = {"op": 1, "error_code": 0} # 表示登录成功 ''' check_uname_pwd(user_name, password) 函数功能:校验用户名和密码是否合法 函数参数: user_name 待校验的用户名 password 待校验的密码 返回值:校验通过返回0,校验失败返回1 ''' if user_reg_login.check_uname_pwd(req["args"]["uname"], req["args"]["passwd"]): # 登录失败 print('登录失败') rsp["error_code"] = 1 rsp_1 = rsp # 将字典转换为字符串在转换为二进制 rsp = json.dumps(rsp).encode() # 求取发送数据的大小,左对齐,宽度15,不足以空白补齐,在转换为二进制 data_len = "{:<15}".format(len(rsp)).encode() # 发送数据大小 sock_conn.send(data_len) # 发送数据 sock_conn.send(rsp) # 如果登录校验成功,则向客户端发送文件 # 此处有修改 if not rsp_1["error_code"]: send_dir(sock_conn) # 关闭连接 sock_conn.close() # 用户注册 elif req["op"] == 2: rsp = {"op": 2, "error_code": 0} # 0表注册成功 if not user_reg_login.user_reg( req["args"]["uname"], req["args"]["passwd"], req["args"]["phone"], req["args"]["email"]): rsp["error_code"] = 1 # 1表注册失败 # 将校验字典转化为字符串,并转为二进制 rsp = json.dumps(rsp).encode() # 求取校验字典的大小,左对齐,宽度15,不足以空白补齐,在转换为二进制 data_len = "{:<15}".format(len(rsp)).encode() # 发送数据大小 sock_conn.send(data_len) # 发送数据 sock_conn.send(rsp) # 关闭套接字 sock_conn.close() # 校验用户名是否存在 elif req["op"] == 3: rsp = {"op": 3, "error_code": 0} # 0表示不存在 # check_user_name(user_name)返回值:校验通过返回0,校验失败返回非零(格式错误返回1,用户名已存在返回2) ret = user_reg_login.check_user_name(req["args"]["uname"]) if ret == 2: rsp["error_code"] = 1 # 1表示存在 print(rsp["error_code"]) # 将校验字典转化为字符串,并转为二进制 rsp = json.dumps(rsp).encode() # 求取校验字典的大小,左对齐,宽度15,不足以空白补齐,在转换为二进制 data_len = "{:<15}".format(len(rsp)).encode() # 发送数据大小 sock_conn.send(data_len) # 发送数据 sock_conn.send(rsp) # 关闭套接字 sock_conn.close()
def user_service_thread(sock_conn): data_len = sock_conn.recv(15).decode().rstrip() if len(data_len) > 0: data_len = int(data_len) recv_size = 0 json_data = b"" while recv_size < data_len: tmp = sock_conn.recv(data_len - recv_size) if tmp == 0: break json_data += tmp recv_size += len(tmp) json_data = json_data.decode() req = json.loads(json_data) if req["op"] == 1: # 登录校验 rsp = {"op": 1, "error_code": 0} if user_reg_login.check_uname_pwd(req["args"]["uname"], req["args"]["passwd"]): rsp["error_code"] = 1 header_data = json.dumps(rsp).encode() data_len = "{:<15}".format(len(header_data)).encode() sock_conn.send(data_len) sock_conn.send(header_data) if not rsp["error_code"]: send_dir(sock_conn) sock_conn.close() elif req["op"] == 2: # 用户注册 rsp = {"op": 2, "error_code": 0} if not user_reg_login.user_reg( req["args"]["uname"], req["args"]["passwd"], req["args"]["phone"], req["args"]["email"]): # 注册失败 rsp["error_code"] = 1 rsp = json.dumps(rsp).encode() data_len = "{:<15}".format(len(rsp)).encode() sock_conn.send(data_len) sock_conn.send(rsp) sock_conn.close() elif req["op"] == 3: # 校验用户名是否存在 rsp = {"op": 3, "error_code": 0} ret = user_reg_login.check_user_name(req["args"]["uname"]) if ret == 2: rsp["error_code"] = 1 rsp = json.dumps(rsp).encode() data_len = "{:<15}".format(len(rsp)).encode() sock_conn.send(data_len) sock_conn.send(rsp) sock_conn.close()