FTP简单的大文件传输
服务器端
# import socket
# import struct
# import json
# sk = socket.socket()
# sk.bind(("127.0.0.1",8090))
# sk.listen()
# buffer = 1024
# conn,addr = sk.accept()
# head_len = conn.recv(4)
# head_len = struct.unpack("i",head_len)[0]
# json_head = conn.recv(head_len).decode("utf-8")
# head = json.loads(json_head)
# filesize = head["filesize"]
# with open(head["filename"],"wb") as f:
# while filesize:
# if filesize >= buffer:
# content = conn.recv(buffer)
# f.write(content)
# filesize -= buffer
# else:
# content = conn.recv(filesize)
# f.write(content)
# break
# conn.close()
# sk.close()
客户端:
# import socket
# import os
# import json
# import struct
# sk = socket.socket()
# sk.connect(("127.0.0.1",8090)) #连接服务端
# buffer = 1024
# #发送文件
# head = {
# "filename":r"员工信息表作业讲解.mp4","filepath":r\'j:\',"filesize":None
#
# }
# file_path = os.path.join(head["filepath"]+head["filename"])
# filesize = os.path.getsize(file_path)
# head["filesize"] = filesize
# json_head = json.dumps(head) #字典转字符串
# bytes_head = json_head.encode("utf-8")#字符串转byets
# head_len = len(bytes_head)
# pack_len = struct.pack("i",head_len)
# sk.send(pack_len)
# sk.send(bytes_head)
# with open(file_path,"rb") as f:
# while filesize:
# if filesize >= buffer:
# content = f.read(buffer)
# sk.send(content)
# filesize -= buffer
# else:
# content = f.read(filesize)
# sk.send(content)
# break
# sk.close()
#
#
#
#
#
#
版权声明:本文为duhong0520原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。