Example #1
0
def check(type):
    user_name = request.form.get("user_name")
    user_pwd = request.form.get("user_pwd")
    msg_type, sender, recipient = type, user_name, "服务器"
    content = {"user_name": user_name, "user_pwd": user_pwd}
    data = (msg_type, sender, recipient, content)
    send_msg(client_socket, data)  # 发送消息到服务器
    size_msg = int(recv_msg(client_socket,
                            15).rstrip())  # 1.接收消息大小,接收信息为字符型,去除右边的空白符,转换为int型
    msg = json.loads(recv_msg(client_socket, size_msg))  # 接收信息,为字符型,将他转换为字典
    return msg
Example #2
0
def check_uname():
    user_name = request.args.get("user_name")
    msg_type, sender, recipient, content = 4, user_name, "服务器", user_name
    data = (msg_type, sender, recipient, content)
    send_msg(client_socket, data)  # 发送消息到服务器
    size_msg = int(recv_msg(client_socket,
                            15).rstrip())  # 1.接收消息大小,接收信息为字符型,去除右边的空白符,转换为int型
    msg = json.loads(recv_msg(client_socket, size_msg))  # 接收信息,为字符型,将他转换为字典
    rsp = {}
    # 用户名存在返回1,不存在返回0
    rsp["err"] = msg["content"]
    return jsonify(rsp)  # 将字典转换为json格式返回
Example #3
0
def SendRecv(type, content):
    '''
    :param type:       消息类型
    :param content:    内容
    :return:           接收的消息,为字典型
    '''
    sender = json.load(open("uname.json", encoding="utf-8"))["user_name"]
    msg_type, recipient = type, "服务器"
    data = (msg_type, sender, recipient, content)
    send_msg(client_socket, data)  # 发送消息到服务器
    size_msg = int(recv_msg(client_socket,
                            15).rstrip())  # 1.接收消息大小,接收信息为字符型,去除右边的空白符,转换为int型
    msg = json.loads(recv_msg(client_socket, size_msg))  # 接收信息,为字符型,将他转换为字典
    return msg
Example #4
0
def R(client_socket):
    try:
        while 1:
            # 先接收消息大小
            data_len = int(hanshu.recv_msg(client_socket, 15).rstrip())
            # 接收内容
            recv_content = json.loads(hanshu.recv_msg(client_socket, data_len))
            if recv_content["type"] == 1:
                # 如果为私聊消息
                dict_chat[1].append(recv_content)
            if recv_content["type"] == 8:
                # 如果为群聊消息
                dict_chat[8].append(recv_content)
            if recv_content["type"] == 2:
                # 如果接收的为用户注册返回信息
                qita_message[2] = recv_content
            if recv_content["type"] == 3:
                # 校验用户名
                qita_message[3] = recv_content
            if recv_content["type"] == 4:
                # 登录信息
                qita_message[4] = recv_content
            if recv_content["type"] == 5:
                # 查看所有用户
                qita_message[5] = recv_content
            if recv_content["type"] == 6:
                # 添加好友请求
                qita_message[6] = recv_content
            if recv_content["type"] == 7:
                # 获取好友列表
                qita_message[7] = recv_content
            if recv_content["type"] == 9:
                # 如果接收的为用户删除好友服务端所返回的消息
                qita_message[9] = recv_content
            if recv_content["type"] == 10:
                # 如果接收的为用户保存个人信息服务端所返回的消息
                qita_message[10] = recv_content
            if recv_content["type"] == 11:
                # 如果接收的为用户提取个人信息服务端所返回的消息
                qita_message[11] = recv_content
    except:
        print("出错了")
Example #5
0
def Addfriends():
    is_login = request.cookies.get("is_login")
    if is_login == "LHL_struggle":
        print("111")
        uname = json.load(open("uname.json", encoding="utf-8"))["user_name"]
        # msg_type = 代表了查看所有用户名
        msg_type, sender, recipient, content = 5, uname, "服务器", "user"
        data = (msg_type, sender, recipient, content)
        send_msg(client_socket, data)  # 发送消息到服务器
        size_msg = int(recv_msg(
            client_socket, 15).rstrip())  # 1.接收消息大小,接收信息为字符型,去除右边的空白符,转换为int型
        msg = json.loads(recv_msg(client_socket,
                                  size_msg))  # 接收信息,为字符型,将他转换为字典
        content = msg["content"]
        print(
            content
        )  # [['123123'], ['123456'], ['1234567'], ['159357'], ['456123']]
        return render_template("Addfriends.html", content=content)
    else:
        return redirect("login.html")
Example #6
0
def message(client_socket, client_address):
    # 与测试聊天对应
    '''
    # 发送信息线程
    threading.Thread(target=F, args=(client_socket,)).start()
    # 接收信息线程
    threading.Thread(target=R, args=(client_socket,)).start()
    '''
    try:
        while 1:
            # 先接收消息大小
            data_len = int(recv_msg(client_socket, 15).rstrip())
            # 接收内容,并转换为字典
            recv_content = json.loads(recv_msg(client_socket, data_len))
            print(recv_content)
            type = recv_content["type"]  # 消息类型
            addresser = recv_content["addresser"]  # 发送人
            recipients = recv_content["recipients"]  # 接收人
            message = recv_content["message"]  # 消息
            '''
            if not client_socket_dict.__contains__(addresser):  # 如果字典中没有这个键值对则
                client_socket_dict[addresser] = client_socket  # 在字典中加入键值对
                uname_list.append(addresser)  # 将用户名添加到在线用户列表中
            '''

            # 私聊请求
            if type == 1:
                # 检验对方是否在线
                if recipients in uname_list:
                    # 如果在线,则将此用户的信息发送给对方
                    to_socket = client_socket_dict[recipients]  # 收信人的套接字
                    send_msg(to_socket, recv_content)  # 发送消息给对方
                else:  # 如果不在线则不发消息给对方,并且回复发送者,对方不在线,请稍后在发送
                    recv_content["addresser"] = "服务器"
                    recv_content["recipients"] = addresser
                    recv_content["message"] = "对方不在线,请稍后在发送"
                    print(recv_content)  # 打印出消息
                    send_msg(client_socket, recv_content)
                    # print("发送成功")

            # 注册请求
            if type == 2:
                user_name = message["user_name"]  # 获取客户端发送过来的用户名
                user_pwd = message["user_pwd"]  # 获取客户端发送过来的密码
                recv_content["addresser"], recv_content[
                    "recipients"] = recipients, addresser
                if check_user_name(user_name) == 1:  # 用户名已存在
                    recv_content["message"] = 1
                if check_user_name(user_name) == 0:  # 用户名不存在
                    # 如果检验过后用户名不存在,则将用户名和用户密码存放到数据库中
                    if save_message(user_name, user_pwd):
                        recv_content["message"] = 0
                    else:
                        recv_content["message"] = 1
                print("要发送的数据:", recv_content)
                send_msg(client_socket, recv_content)  # 发送消息给客户端 0注册成功,1注册失败

            # 校验用户名
            if type == 3:
                recv_content["addresser"], recv_content[
                    "recipients"], recv_content[
                        "message"] = recipients, addresser, check_user_name(
                            message)
                print(recv_content["message"])
                send_msg(client_socket, recv_content)  #  发送消息给客户端 1存在,0不存在

            # 登录请求
            if type == 4:
                user_name = message["user_name"]  # 获取客户端发送过来的用户名
                user_pwd = message["user_pwd"]  # 获取客户端发送过来的密码
                # 将登录成功的用户,添加到在线列表中
                if check_user_pwd(user_name, user_pwd) == 0:
                    if not client_socket_dict.__contains__(
                            addresser):  # 如果字典中没有这个键值对则
                        client_socket_dict[
                            addresser] = client_socket  # 在字典中加入键值对
                        uname_list.append(addresser)  # 将用户名添加到在线用户列表中
                recv_content["addresser"], recv_content[
                    "recipients"], recv_content[
                        "message"] = recipients, addresser, check_user_pwd(
                            user_name, user_pwd)
                send_msg(client_socket, recv_content)  #  发送消息给客户端 0登录成功,1登录失败

            # 查看用户请求
            if type == 5:
                recv_content["addresser"], recv_content[
                    "recipients"], recv_content[
                        "message"] = recipients, addresser, find_uname(
                            addresser)
                print(recv_content["message"])
                send_msg(client_socket, recv_content)  # 响应客户端

            # 添加好友
            if type == 6:
                # friend.json文件为好友集合文件
                friends_dict = json.load(open("friend.json", encoding="utf-8"))
                if not friends_dict.__contains__(
                        addresser):  # 判断文件中是否有关于这个用户的好友列表
                    # 如果没有添加这个用户的好友列表,就添加新的键值对,值由列表组成
                    friends_dict[addresser] = [message]
                    recv_content["message"] = "添加成功"
                else:  # 如果有则将新好友导入列表
                    if message in friends_dict[addresser]:
                        recv_content["message"] = "已为好友"
                    else:
                        friends_dict[addresser].append(message)
                        recv_content["message"] = "添加成功"
                with open("friend.json", "wb") as f:
                    f.write(json.dumps(friends_dict).encode()
                            )  # 将字典转换为字符串,再将字符串转换为字节型的写入friend.json中
                recv_content["addresser"], recv_content[
                    "recipients"] = recipients, addresser
                send_msg(client_socket, recv_content)  # 响应客户端

            # 获取好友列表
            if type == 7:
                friends_dict = json.load(open(
                    "friend.json", encoding="utf-8"))  # 读取json文件好友列表信息
                recv_content["addresser"], recv_content[
                    "recipients"] = recipients, addresser
                if not friends_dict.__contains__(addresser):  # 没有好友
                    recv_content["message"] = ["你还没有好友"]
                else:
                    recv_content["message"] = friends_dict[addresser]
                send_msg(client_socket, recv_content)  # 响应客户端

            # 删除好友请求
            if type == 9:
                friends_dict = json.load(open("friend.json",
                                              encoding="utf-8"))  # 读取存放好友列表的文件
                message = recv_content["message"]  # 消息
                recv_content["addresser"], recv_content[
                    "recipients"] = recipients, addresser
                if message in friends_dict[addresser]:  # 如果好友在列表中则删除好友
                    friends_dict[addresser].remove(message)  # 删除好友
                    with open("friend.json", "wb") as f:  # 打开文件
                        f.write(json.dumps(friends_dict).encode())  # 写入修改过的信息
                    recv_content["message"] = "删除成功"
                else:
                    recv_content["message"] = "没有该好友"
                send_msg(client_socket, recv_content)  # 响应客户端

            # 群聊请求
            if type == 8:
                # 遍历在线用户列表
                for x in uname_list:
                    if x != addresser:  # 发送消息给除开自己的用户
                        to_socket = client_socket_dict[x]  # 收信人的套接字
                        send_msg(to_socket, recv_content)  # 发送消息给对方

            # 保存个人信息
            if type == 10:
                # message  接收到的个人信息
                # save_per_infor(message)  1 表示保存或修改成功,0表是未修改
                recv_content["addresser"], recv_content[
                    "recipients"], recv_content[
                        "message"] = recipients, addresser, save_per_infor(
                            message)
                send_msg(client_socket, recv_content)  # 响应客户端

            # 提取个人信息
            if type == 11:
                # message ,表示个人信息的用户名,并不一定是自己的,也有可能是好友的
                PerInfor = extract_per_infor(
                    message)  # PerInfor 值为None或一个字典类型的数据
                recv_content["addresser"], recv_content[
                    "recipients"], recv_content[
                        "message"] = recipients, addresser, PerInfor
                send_msg(client_socket, recv_content)  # 响应客户端

    except:
        print("出错了")
        # 如果用户退出登录则,删除字典和列表中的值
        if client_socket_dict.__contains__(addresser):
            client_socket_dict.pop(addresser)  # 删除字典中的值
        if addresser in uname_list:
            uname_list.remove(addresser)
        client_socket.close()  # 关闭客户端套接字
Example #7
0
def chat(conn_socket, conn_address):
    try:
        while 1:
            print("1")
            # 接收信息
            size_msg = int(
                recv_msg(conn_socket,
                         15).rstrip())  # 1.接收消息大小,接收信息为字符型,去除右边的空白符,转换为int型
            str_msg = recv_msg(conn_socket, size_msg)  # 接收信息,为字符型
            msg = json.loads(str_msg)  # 转换为字典
            print("5", msg)
            msg_type = msg["msg_type"]  # 消息类型
            sender = msg["sender"]  # 发信人
            recipient = msg["recipient"]  # 收信人
            content = msg["content"]  # 接收的信息

            # 聊天请求
            if msg_type == 1:
                if not client_socks_dict.__contains__(sender):  # 如果
                    client_socks_dict[sender] = conn_socket  # 存用户名与客户端的套接字
                    uname_list.append(sender)  # 导入用户名
                print("在线用户:", uname_list)
                # 校验对方是否在线,在线则把用户的消息发给对方
                if recipient in uname_list:
                    to_client = client_socks_dict[recipient]  # 收信人对应的套接字
                    data = (msg_type, sender, recipient, content)  # 信息
                    send_msg(to_client, data)  # 发送消息
                else:
                    # 不再线
                    sender = "服务器"
                    content = "对方不在线"
                    data = (msg_type, sender, recipient, content)
                    send_msg(conn_socket, data)  # 发送消息, data为元组

            # 注册请求
            if msg_type == 2:
                print(content, type(content))
                user_name = content["user_name"]
                user_pwd = content["user_pwd"]
                # 1 校验用户名是否存在, 校验通过返回0,用户名存在返回1
                if check_user_name(user_name) == 1:  # 用户名已存在
                    recipient = sender
                    sender = "服务器"
                    content = 1  # 表用户名已存在
                    data = (msg_type, sender, recipient, content)
                    send_msg(conn_socket, data)  # 给客户端返回信息
                if check_user_name(user_name) == 0:  # 用户名不存在
                    recipient = sender
                    sender = "服务器"
                    # 成功返回True,失败返回False
                    if user_reg(user_name, user_pwd):  # 将注册信息存入数据库
                        content = 0  # 注册成功
                    else:
                        content = 2  # 注册失败
                    data = (msg_type, sender, recipient, content)
                    send_msg(conn_socket, data)  # 给客户的返回消息 content = 0注册成功,2失败

            # 登陆请求
            if msg_type == 3:
                print(content, type(content))
                user_name = content["user_name"]
                user_pwd = content["user_pwd"]
                recipient = sender
                sender = "服务器"
                # 校验通过返回0,校验失败返回1
                content = 1  # 默认校验失败
                content = check_uname_pwd(user_name, user_pwd)  # 用户名及密码通过验证
                data = (msg_type, sender, recipient, content)
                send_msg(conn_socket, data)  # 给客户的返回消息 content = 0校验成功,1校验失败

            # 校验用户名请求
            if msg_type == 4:
                print(content, type(content))
                recipient = sender
                sender = "服务器"
                # 1 校验用户名是否存在, 不存在返回0,用户名存在返回1
                if check_user_name(content) == 1:  # 用户名已存在
                    content = 1  # 表用户名已存在
                else:  # 用户名不存在
                    content = 0
                data = (msg_type, sender, recipient, content)
                send_msg(conn_socket, data)  # 给客户端返回信息

            # 查看用户名请求
            if msg_type == 5:
                recipient = sender
                sender = "服务器"
                content = find_uname()
                print(content, type(content))
                data = (msg_type, sender, recipient, content)
                send_msg(conn_socket, data)  # 给客户端返回信息

            # 添加好友请求
            if msg_type == 6:
                # friend.json 文件为好友集合文件,
                friends_dict = json.load(open("friend.json", encoding="utf-8"))
                if content not in add_fri:  # 如果以添加改好友
                    add_fri.append(content)  # 导入好友
                    friends_dict[sender] = add_fri  # 放入字典
                    with open("friend.json", "wb") as f:
                        f.write(json.dumps(
                            friends_dict).encode())  # 将字典转换为字符串,在转换为字节型写入文件
                    content = "添加成功"
                else:
                    content = "已为好友"
                recipient = sender
                sender = "服务器"
                data = (msg_type, sender, recipient, content)
                print(data)
                send_msg(conn_socket, data)  # 给客户端返回信息

            # 获得已添加好友请求
            if msg_type == 7:
                friends_dict = json.load(open("friend.json",
                                              encoding="utf-8"))  # 读取文件内容
                if friends_dict.__contains__(sender):  # 判断是否有好友存在
                    content = friends_dict[sender]  # 返回列表
                else:
                    content = ["你还没有好友"]
                recipient = sender
                sender = "服务器"
                data = (msg_type, sender, recipient, content)
                print(data)
                send_msg(conn_socket, data)  # 给客户端返回信息

    except Exception as b:
        # print(b)
        print("出错了")
        # 如果用户退出登录,则删除字典和列表中的值
        client_socks_dict.pop(sender)
        uname_list.remove(sender)
        print("在线用户:", uname_list)
        conn_socket.close()