飞鸽传书-下载文件-存下载文件信息

FeiQRecv.py(修改部分如下)

def recv_msg():
    """接收数据"""
    while True:
        recv_data, dest_addr = FeiQCoreData.udp_socket.recvfrom(1024)
        # print("(接收到的数据)%s>>>%s" % (str(dest_addr), recv_data.decode("gbk")))

        feiq_data = deal_with_recv_msg(recv_data)
        # print("(处理之后的数据)", feiq_data)

        # 0000 0001
        # 0000 0010
        # 0000 0100
        #
        # 0000 0001, 0000 0010, 0000 0100
        #
        # 0000 0111

        command, option = get_command_option(feiq_data['command_num'])
        # print(command, option)

        if command == FeiQCoreData.IPMSG_BR_ENTRY:
            # 用户上线
            print("%s上线" % feiq_data['user_name'])


            find_position = feiq_data['option'].find("\0")
            if find_position != -1:
                user_name = feiq_data['option'][:find_position]
            else:
                user_name = feiq_data['option']

            new_user_info = dict()
            new_user_info['ip'] = dest_addr[0]
            new_user_info['user_name'] = user_name

            if new_user_info not in FeiQCoreData.user_list:
                FeiQCoreData.user_list.append(new_user_info)

            # 通报给对方 我已在线
            answer_online_msg = FeiQSend.build_msg(FeiQCoreData.IPMSG_ANSENTRY)
            FeiQSend.send_msg(answer_online_msg, dest_addr[0])

        elif command == FeiQCoreData.IPMSG_BR_EXIT:
            # 用户下线
            print("%s下线" % feiq_data['user_name'])

            for user_info in FeiQCoreData.user_list:
                if dest_addr[0] == user_info['ip']:
                    FeiQCoreData.user_list.remove(user_info)
                    break

        elif command == FeiQCoreData.IPMSG_ANSENTRY:
            # 其他用户通报在线
            print("%s已经在线" % feiq_data['user_name'])

            find_position = feiq_data['option'].find("\0")
            if find_position != -1:
                user_name = feiq_data['option'][:find_position]
            else:
                user_name = feiq_data['option']

            new_user_info = dict()
            new_user_info['ip'] = dest_addr[0]
            new_user_info['user_name'] = user_name
            if new_user_info not in FeiQCoreData.user_list:
                FeiQCoreData.user_list.append(new_user_info)

        elif command == FeiQCoreData.IPMSG_SENDMSG:
            # 其他用户发送过来新的消息

            # -------添加------
            # 判断如果发送过来的是文件消息
            if option & 0x00f00000 == FeiQCoreData.IPMSG_FILEATTACHOPT:
                # 飞鸽传书中的选项为  \0 文件序号:文件名:文件大小:文件修改时间:文件类型:
                file_info_msg = dict()
                # 跳过\0, 当然了如果对方发送过来的还包括普通的消息,那么就需要find查找
                file_info_list = feiq_data['option'][1:].split(":", 5)
                file_info_msg['packet_id'] = int(feiq_data['packet_id'])
                file_info_msg['file_id'] = int(file_info_list[0])
                file_info_msg['file_name'] = file_info_list[1]
                file_info_msg['file_size'] = int(file_info_list[2], 16)
                file_info_msg['dest_ip'] = dest_addr[0]
                FeiQCoreData.download_file_list.append(file_info_msg)

            else:
                print("%s(%s)>>>%s" % (feiq_data['user_name'], str(dest_addr), feiq_data['option']))

            # 告知对方已经接收到数据
            recv_ok_msg = FeiQSend.build_msg(FeiQCoreData.IPMSG_RECVMSG)
            FeiQSend.send_msg(recv_ok_msg, dest_addr[0])

FeiQCoreData.py(修改部分如下)

udp_socket = None  # 保存udp套接字
feiq_version = 1  # 飞秋的版本
feiq_user_name = "dong-test"  # 用户名
feiq_host_name = "ubuntu-64-1604"  # 主机名字
broadcast_ip = "255.255.255.255"  # 广播ip
feiq_port = 2425  # 飞鸽传书的端口

# 飞秋command
IPMSG_BR_ENTRY = 0x00000001  # 上线
IPMSG_BR_EXIT = 0x00000002  # 下线
IPMSG_SENDMSG = 0x00000020  # 发送 消息
IPMSG_ANSENTRY = 0x00000003  # 应答在线
IPMSG_RECVMSG = 0x00000021  # 告知对方 已收到消息

IPMSG_FILEATTACHOPT = 0x00200000  # 表示文件消息
IPMSG_FILE_REGULAR = 0x00000001  # 表示普通文件

user_list = list()  # 保存在线用户的列表

download_file_list = list()  # 用来保存需要现在的文件 # -------添加---------

file_queue = None
packet_id = 0

main.py (修改部分如下)


# -------添加---------
def print_all_waiting_files():
    """显示可以下载的文件"""
    for i, file_info in enumerate(FeiQCoreData.download_file_list):
        print(i, file_info)


def print_menu():
    """显示飞鸽传书的功能"""
    print("     飞鸽传书v1.0")
    print("1:上线广播")
    print("2:下线广播")
    print("3:给指定的ip发送数据")
    print("4:显示在线用户信息")
    print("5:给指定的ip发送文件")
    print("6:显示可以下载文件")  # -------添加---------
    print("0:退出")


def main():

    FeiQCoreData.file_queue = multiprocessing.Queue()

    tcp_process = multiprocessing.Process(target=FeiQTcp.tcp_main, args=(FeiQCoreData.file_queue,))
    tcp_process.start()

    # 创建套接字
    create_udp_socket()

    # 创建一个子线程,接收数据
    recv_msg_thread = threading.Thread(target=FeiQRecv.recv_msg)
    recv_msg_thread.start()

    while True:

        print_menu()
        command_num = input("请输入要进行的操作:")
        if command_num == "1":
            # 发送上线提醒
            FeiQSend.send_broadcast_online_msg()
        elif command_num == "2":
            # 发送下线提醒
            FeiQSend.send_broadcast_offline_msg()
        elif command_num == "3":
            # 发送消息
            FeiQSend.send_chat_msg()
        elif command_num == "4":
            # 显示在线用户信息
            print_online_user()
        elif command_num == "5":
            # 给指定的ip发送文件
            FeiQSend.send_file_msg()
        elif command_num == "6":  # -------添加---------
            # 显示可以下载的文件
            print_all_waiting_files()
        elif command_num == "0":
            FeiQSend.send_broadcast_offline_msg()
            # 关闭套接字
            FeiQCoreData.udp_socket.close()
            exit()

其他文件未修改