网络编程--大作业

import socket import struct import os import json sk = socket.socket() sk.bind(('127.0.0.1',8090)) sk.listen() buffer = 1024 conn,addr = sk.accept() # 接收文件目录 res_filepath = conn.recv(4) struct_filepath = struct.unpack('i',res_filepath)[0] res_filepath = conn.recv(struct_filepath).decode('utf-8') # res_filepath = conn.recv(1024).decode('utf-8') # json_filepath = json.loads(res_filepath) new_filepath = res_filepath+'\\'+'test2' rnew_dir = os.mkdir(new_filepath) # 接收文件名并创建文件 res_filename = conn.recv(4) struct_filename = struct.unpack('i',res_filename)[0] filename = conn.recv(struct_filename).decode('utf-8') # 接收文件大小 res_filesize = conn.recv(4) struct_filesize = struct.unpack('i',res_filesize)[0] #开始文件写入 with open(new_filepath+'\\'+filename,'wb') as f: while True: if struct_filesize >= buffer: connnect = conn.recv(buffer) f.write(connnect) struct_filesize -= buffer else: connnect = conn.recv(struct_filesize) f.write(connnect) break conn.close() sk.close()
import socket import os import struct import json sk = socket.socket() sk.connect(('127.0.0.1',8090)) buffer = 1024 # 先找到文件目录位置 head = {'filepath':r"C:\Users\allen\Desktop\python\day32", 'filename':r'1.struct.py', 'filesize':None} file = os.path.join(head['filepath'],head['filename']) # 发送文件目录到服务端 len_filepath = len(head['filepath']) struct_filepath = struct.pack('i',len_filepath) sk.send(struct_filepath) # filepath = json.dumps(head['filepath']).encode('utf-8') filepath = head['filepath'].encode('utf-8') send_filepath = sk.send(filepath) # 发送文件名到服务端 len_filename = len(head['filename']) struct_filename = struct.pack('i',len_filename) sk.send(struct_filename) # 发送文件大小到服务端 head['filesize'] = os.path.getsize(file) filesize = head['filesize'] # 开始进行文件大小发送 struct_filesize = struct.pack('i',head['filesize']) sk.send(head['filename'].encode('utf-8')) sk.send(struct_filesize) with open(file,'rb') as f: while True: if filesize >= buffer: content = f.read(buffer) sk.send(content) filesize -= buffer else: content = f.read(filesize) sk.send(content) break sk.close()
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 True: 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'01 python fullstack s9day31 复习和认识tcp的长连接.mp4', 'filepath':r'D:\jerry\python全栈\Python全栈9期(第一部分):基础\day31', 'filesize':None} filepath = os.path.join(head['filepath'],head['filename']) filesize = os.path.getsize(filepath) head['filesize'] = filesize json_head = json.dumps(head) # 字典转成了字符串 bytes_head = json_head.encode('utf-8') # 字符串转bytes # 计算head的长度 head_len = len(bytes_head) # 计算报头长度 struct_head = struct.pack('i',head_len) sk.send(struct_head) # 先发报头长度 sk.send(bytes_head) # 再发bytes数据类型的报头 # 发送文件内容 with open(filepath,'rb') as f: while True: if filesize >= buffer: content = f.read(buffer) # 每次读buffer长度的内容 sk.send(content) filesize -= buffer else: content = f.read(filesize) sk.send(content) break sk.close()
浙公网安备 33010602011771号