• 作业26


    作业26

    socket实现网络下载功能

    (此版本为服务器是unix系统,客户端为windows系统)

    客户端

    import socket
    import json
    import struct
    import os
    
    obj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    obj.connect(("139.196.38.98", 8080))
    # C:UserswwhDesktop上课笔记上课代码day16作业
    while 1:
        cmd = input("请输入要下载的文件的绝对路径===>").strip()
        if cmd.lower() == "q":
            break
        if len(cmd) == 0:
            print('不能输入空')
            continue
        obj.send(cmd.encode("utf-8"))
        header_len_bytes = obj.recv(4)
        header_len = struct.unpack("i", header_len_bytes)
        header_bytes = obj.recv(header_len[0])
        header = json.loads(header_bytes)
        print(header)
        total_size = header["total_size"]
        file_name = header["file_name"]
        print(file_name)
        size = 0
        path = "C:\Users\wwh\Desktop\" + r"{}".format(file_name)
        while total_size > size:
            data = obj.recv(1024)
            size += len(data)
            # print(path)
            path = os.path.normcase(path)
            # print(path)
            print("
    ",size/total_size)
    
            if not os.path.exists(path):
                os.mkdir(path)
            with open(path, "ab") as f:
                f.write(data)
        obj.close()
    
    # //tmp/Python-3.8.2
    

    服务端

    import json
    import socketserver
    import struct
    import os
    
    
    class MyRequest(socketserver.BaseRequestHandler):
        def handle(self):
            # self.request就是conn
            while 1:
                try:
                    path_bytes = self.request.recv(1024)
                    if len(path_bytes) == 0:
                        break
                    down_path = path_bytes.decode("utf-8")
                    file_name = down_path.split("/")[-1]
                    total_size = os.path.getsize(r"{}".format(down_path))
                    header = {"total_size": total_size, "file_name": file_name}
                    header_str = json.dumps(header)
                    header_bytes = header_str.encode("utf-8")
                    header_size_bytes = struct.pack("i", len(header_bytes))
                    print(header_size_bytes, header_bytes, header_str)
                    self.request.send(header_size_bytes)
                    self.request.send(header_bytes)
                    print(f"{self.client_address}发来下载请求:{down_path},文件大小为{total_size}")
    
                    with open(down_path, "rb") as f:
                        for line in f:
                            self.request.send(line)
                except Exception:
                    break
            self.request.close()
    
    
    # obj = socketserver.ThreadingTCPServer(('127.0.0.1', 8080), MyRequest)
    obj = socketserver.ThreadingTCPServer(('0.0.0.0', 8080), MyRequest)
    obj.serve_forever()
    
    
  • 相关阅读:
    Mac下空格预览.webp格式图片
    stl 存放对象析构问题
    初始化成员列表 ——— 类的const成员和引用成员的初始化
    一个CString的实现 拷贝构造函数的应用
    operator 的两种用法
    MFC使用TRACKMOUSEEVENT触发mouseHover和mouseLeave
    MFC重载关闭按钮
    类内定义线程的回调函数问题
    按钮的Default Button属性
    匿名管道 远程cmd
  • 原文地址:https://www.cnblogs.com/achai222/p/12748348.html
Copyright © 2020-2023  润新知