码迷,mamicode.com
首页 > 其他好文 > 详细

No.4 文件断点续传(Server)

时间:2019-07-30 18:52:36      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:断点   sockets   完整   cli   open   style   需要   verify   sock   

import socketserver, os , json , struct, hashlib

codes = {
    "文件已存在":1001,
    "文件不存在":1002
}

File_Path = rC:\Users\17303\Desktop\电脑的组成.avi #需要传的文件路径
File_Size = os.path.getsize(File_Path)

f = open(File_Path,"rb")
md5 = hashlib.md5()
md5.update(f.read())
md5_val = md5.hexdigest()

File_info = {
    "FileName":"电脑的组成.avi",
    "FileSize":File_Size,
    "md5_val":md5_val
}
"""
先开始
1.发送字典的长度;
2.发送字典 (Filename,文件md5值,FileSize)
3.接收字典 (Code,FileSize)/(Code,FileSize(0))
4.发送文件 (seek(FIleSize))
5.校验完整性
"""
File_info_json = json.dumps(File_info).encode("utf-8")
File_info_json_length = struct.pack("i",len(File_info_json))

class My(socketserver.BaseRequestHandler):
    
    def handle(self):
        self.request.send(File_info_json_length)
        self.request.send(File_info_json)
        response = json.loads(self.request.recv(1024).decode("utf-8"))
        
        Code = response["Code"]
        Md5_Size = response["Md5_Size"]
        self.send_info(File_Path,Md5_Size)

        client_md5 = self.request.recv(1024).decode("utf-8")

        self.verify(client_md5,md5_val)

    def send_info(self, File_Path, Md5_Size):
        f = open(File_Path,"rb")
        f.seek(Md5_Size)
        for line in f:
            self.request.send(line)

    def verify(self, client_md5, md5_val):
        if md5_val == client_md5:
            self.request.send(b200)

        else:
            self.request.send(b404)

server = socketserver.ThreadingTCPServer(("127.0.0.1",8080,), My)
server.serve_forever()

 

No.4 文件断点续传(Server)

标签:断点   sockets   完整   cli   open   style   需要   verify   sock   

原文地址:https://www.cnblogs.com/deng-fei/p/11271938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!