Beispiel #1
0
def save_message():
    if request.cookies.get("is_login") == "LHL_struggle":
        if request.method == "GET":
            addresser = json.load(open("uname.json", encoding="utf-8"))["uname"]  # 自己的用户名
            return render_template("save_message.html", uname=addresser)
        if request.method == "POST":
            print("进入了post请求")
            addresser = json.load(open("uname.json", encoding="utf-8"))["uname"]  # 自己的用户名
            sign = request.form.get("sign")            # 获取表单中的签名(70)
            gender = request.form.get("gender")        # 获取性别(2)
            birthday = request.form.get("birthday")    # 获取生日(20)
            home = request.form.get("home")            # 所在地(50)
            school = request.form.get("school")        # 在读院校(40)
            professional = request.form.get("professional")  # 职业(40)
            elucidation = request.form.get("elucidation")    # 个人说明(150字)
            PerInfor = {"addresser": addresser, "sign": sign, "gender": gender, "birthday": birthday, "home": home, "school": school, "professional": professional, "elucidation": elucidation}
            send_dict = {"type": 10, "addresser": addresser, "recipients": "服务器", "message": PerInfor}
            print("获取到的数据%s" % PerInfor)
            hanshu.send_msg(client_socket, send_dict)     # 发送消息给服务端
            # 接收服务端的信息
            recv_content = qita_jieshu(10)    # 从字典中获取信息
            if recv_content["message"] == 1:   # 1表示保存成功,0代表未修改
                return "保存成功"
            else:
                return "保存失败"
    else:
        return render_template("/login")
Beispiel #2
0
def s_group_msg():
    try:
        addresser = json.load(open("uname.json", encoding="utf-8"))["uname"]  # 自己的用户名
        send_dict = {"type": 8, "addresser": addresser, "recipients": "all"}
        send_dict["message"] = request.args.get("message")    # 获取要发送的内容
        hanshu.send_msg(client_socket, send_dict)   # 发送消息
        return jsonify({"result": 1})    # 发送成功返回1
    except:
        return jsonify({"result": 0})    # 发送失败返回0
Beispiel #3
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
Beispiel #4
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格式返回
Beispiel #5
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
Beispiel #6
0
def personal_show():
    addresser = json.load(open("uname.json", encoding="utf-8"))["uname"]  # 获取用户名
    uname = request.args.get("uname")   # 获取需要查看用户的用户名
    send_dict = {"type": 11, "addresser": addresser, "recipients": "服务器", "message": uname}
    hanshu.send_msg(client_socket, send_dict)  # 发送消息给服务端
    # 接收服务端的信息
    recv_content = qita_jieshu(11)  # 从字典中获取信息
    if recv_content["message"]:    # 如果recv_content["message"]不为None
        # 所要查看个人信息的用户名加入到个人信息中
        recv_content["message"]["addresser"] = uname
        print(recv_content["message"])     # 打印出个人信息
        return render_template("personal_show.html", message=recv_content["message"])
    else:      # 如果为None
        PerInfor = {"addresser": uname, "sign": None, "gender": None, "birthday": None, "home": None, "school": None, "professional": None, "elucidation": None}
        return render_template("personal_show.html", message=PerInfor)
Beispiel #7
0
def addf():
    uname = json.load(open("uname.json", encoding="utf-8"))["uname"]   # 自己的用户名
    friend_name = request.args.get("uname")   # 得到对方用户名
    send_dict = {"type": 6, "addresser": uname, "recipients": "服务器", "message": friend_name}
    hanshu.send_msg(client_socket, send_dict)  # 发送消息

    # 从存放信息的字典中,获取对应的信息,如果没有该信息,则值为0
    recv_content = 0
    while recv_content == 0:
        # recv_content 为接收到的信息
        recv_content = qita_message.get(6, 0)
    # 获取数据后,删除该键值对
    del qita_message[6]

    return recv_content["message"]
Beispiel #8
0
def check_uname():
    user_name = request.args.get("user_name")
    content_dict = {"type": 3, "addresser": user_name, "recipients": "服务器", "message": user_name}
    hanshu.send_msg(client_socket, content_dict)  # 放送消息给服务器

    # 从存放信息的字典中,获取对应的信息,如果没有该信息,则值为0
    recv_content = 0
    while recv_content == 0:
        # recv_content 为接收到的信息
        recv_content = qita_message.get(3, 0)
    # 获取数据后,删除该键值对
    del qita_message[3]
    print(recv_content["message"])
    # 用户名存在返回1,不存在返回0
    return jsonify({"err": recv_content["message"]})    # 将字典转换为json格式返回
Beispiel #9
0
def check(type):
    user_name = request.form.get("user_name")  # 获取用户名
    user_pwd = request.form.get("user_pwd")  # 获取用户密码
    content = {"user_name": user_name, "user_pwd": user_pwd}  # 将消息组成字典成为发送内容
    content_dict = {"type": type, "addresser": user_name, "recipients": "服务器", "message": content}
    hanshu.send_msg(client_socket, content_dict)  # 放送消息给服务器
    # 等待服务端消息

    # 从存放信息的字典中,获取对应的信息,如果没有该信息,则值为0
    recv_content = 0
    while recv_content == 0:
        recv_content = qita_message.get(type, 0)
    # 获取数据后,删除该键值对
    del qita_message[type]
    # 返回数据
    return recv_content
Beispiel #10
0
def del_friend():
    if request.cookies.get("is_login") == "LHL_struggle":
        uname = json.load(open("uname.json", encoding="utf-8"))["uname"]  # 自己的用户名
        FriendName = request.args.get("friend_name")   # 获取要删除的好友用户名
        del_FriendName = {"type": 9, "addresser": uname, "recipients": "服务器", "message": FriendName}   # 删除请求消息
        hanshu.send_msg(client_socket, del_FriendName)  # 发送消息

        # 接收删除好友请求,所返回的消息
        # 从存放信息的字典中,获取对应的信息,如果没有该信息,则值为0
        recv_content = 0
        while recv_content == 0:
            # recv_content 为接收到的信息
            recv_content = qita_message.get(9, 0)
        # 获取数据后,删除该键值对
        del qita_message[9]
        return recv_content["message"]
    else:
        return render_template("/login")
Beispiel #11
0
def friends():
    if request.cookies.get("is_login") == "LHL_struggle":
        uname = json.load(open("uname.json", encoding="utf-8"))["uname"]  # 自己的用户名
        send_dict = {"type": 7, "addresser": uname, "recipients": "服务器", "message": ""}
        hanshu.send_msg(client_socket, send_dict)  # 发送消息

        # 从存放信息的字典中,获取对应的信息,如果没有该信息,则值为0
        recv_content = 0
        while recv_content == 0:
            # recv_content 为接收到的信息
            recv_content = qita_message.get(7, 0)
        # 获取数据后,删除该键值对
        del qita_message[7]

        print(recv_content["message"])
        return render_template("friends.html", content=recv_content["message"])
    else:
        return render_template("/login")
Beispiel #12
0
def Addfriends():
    if request.cookies.get("is_login") == "LHL_struggle":
        uname = json.load(open("uname.json", encoding="utf-8"))["uname"]
        send_dict = {"type": 5, "addresser": uname, "recipients": "服务器", "message": ""}
        hanshu.send_msg(client_socket, send_dict)  # 向服务器发出请求

        # 从存放信息的字典中,获取对应的信息,如果没有该信息,则值为0
        recv_content = 0
        while recv_content == 0:
            # recv_content 为接收到的信息
            recv_content = qita_message.get(5, 0)
        # 获取数据后,删除该键值对
        del qita_message[5]

        message = recv_content["message"]
        print(message)      # [['123123'], ['123456'], ['1234567'], ['159357'], ['456123']]
        return render_template("Addfriends.html", content=message)
    else:
        return render_template("/login")
Beispiel #13
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")
Beispiel #14
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()  # 关闭客户端套接字
Beispiel #15
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()