• 作业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()
    
    
  • 相关阅读:
    MyCat 数据库读写分离
    python 网络编程-05 socketserver
    python 网络编程-04 文件的传输
    python 网络编程-03 粘包问题及处理
    常用模块-01getopt
    js获取自动获取本电脑IP
    js变态需求
    js正则不能输入中文和英文
    禁止输入中文
    手机号3-4-5
  • 原文地址:https://www.cnblogs.com/achai222/p/12748348.html
Copyright © 2020-2023  润新知