飞鸽传书-发送下载、接收文件到Queue

FeiQSend.py(修改部分代码)


def send_file_msg():
    """给指定的ip发送文件"""

    # 1:123123:dongge:ubuntu:文件消息命令字:消息内容(可以没有) \0 0:hello.py:123:12123:文件类型:

    # 获取对方的ip
    dest_ip = input("请输入对方的ip(输入0显示当前所有的在线用户):")
    if dest_ip == "0":
        # 显示用户列表
        print("="*30)
        for i, user_info in enumerate(FeiQCoreData.user_list):
            print(i, user_info)
        print("="*30)

        # 从中取一个用户的ip
        try:
            num = int(input("请输入用户对应的序号:"))
        except:
            print("输入有误...")
            return
        else:
            dest_ip = FeiQCoreData.user_list[num]['ip']

    # 获取要发送的文件名
    file_name = input("请输入要发送的文件名:")

    # 判断是否有目的ip以及要发送的内容,有则发送
    if dest_ip and file_name:

        try:
            file_size = os.path.getsize(file_name)
            file_ctime = os.path.getctime(file_name)
        except:
            print("没有此文件。。。。")
        else:
            # 文件序号:文件名:文件大小:文件修改时间:文件类型:
            option = "%d:%s:%x:%x:%x:" % (0, file_name, file_size, int(file_ctime), FeiQCoreData.IPMSG_FILE_REGULAR)

            option = "\0" + option


            file_msg = build_msg(FeiQCoreData.IPMSG_SENDMSG|FeiQCoreData.IPMSG_FILEATTACHOPT, option)
            send_msg(file_msg, dest_ip)


            # 向子进程中发送包编号/文件序号/文件名
            send_file_info = dict()
            send_file_info['packet_id'] = FeiQCoreData.packet_id
            send_file_info['file_id'] = 0
            send_file_info['file_name'] = file_name

            # -------添加---------
            queue_info = dict()
            queue_info['type'] = "send_file"  # 添加一个key-value用来标记类型
            queue_info['data'] = send_file_info

            FeiQCoreData.file_queue.put(queue_info)


# -------添加---------
def download_file():
    """下载文件"""

    for i, file_info in enumerate(FeiQCoreData.download_file_list):
        print(i, file_info)

    try:
        num = int(input("请输入要下载的文件序号:"))
    except:
        print("输入数据有误....")
        return
    else:
        file_info = FeiQCoreData.download_file_list[num]

        queue_info = dict()
        queue_info['type'] = "download_file"  # 添加一个key-value用来标记类型
        queue_info['data'] = file_info

        # 发送到Queue中
        FeiQCoreData.file_queue.put(queue_info)

main.py


def print_menu():
    """显示飞鸽传书的功能"""
    print("     飞鸽传书v1.0")
    print("1:上线广播")
    print("2:下线广播")
    print("3:给指定的ip发送数据")
    print("4:显示在线用户信息")
    print("5:给指定的ip发送文件")
    print("6:显示可以下载文件")
    print("7:下载文件")  # -------添加---------
    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 == "7":   # -------添加---------
            # 下载文件
            FeiQSend.download_file()
        elif command_num == "0":
            FeiQSend.send_broadcast_offline_msg()
            # 关闭套接字
            FeiQCoreData.udp_socket.close()
            exit()