---恢复内容开始---
socket粘包:两个包连在一起了
解决办法,在两个包之间的信息接收一个返回信息
import socket import os # import time server = socket.socket() server.bind(('localhost',6969)) server.listen() while True: conn,addr = server.accept() print("new conn :",addr) while True: data = conn.recv(1024) if not data: print("客户端已经断开") break print("执行指令:",data) cmd_res = os.popen(data.decode('utf-8')).read() if len(cmd_res) == 0: cmd_res = "cmd has no output,,," conn.send(str(len(cmd_res.encode('utf-8'))).encode('utf-8')) # time.sleep(0.5) '''-----------------------------------''' client_ack = conn.recv(1024) # wait client to continue '''-----------------------------------''' print("client_ack:",client_ack.decode()) conn.send(cmd_res.encode('utf-8')) print("send done") server.close()
#客户端 import socket client = socket.socket() client.connect(('localhost',6969)) while True: cmd = input(">>:").strip() if len(cmd) == 0: continue client.send(cmd.encode('utf-8')) cmd_res_size = client.recv(1024) print('命令结果大小:',cmd_res_size.decode()) '''-------------------''' client.send("准备好啦".encode("utf-8")) '''-------------------''' size = 0 receive_data = b'' while size < int(cmd_res_size.decode()): data = client.recv(1024) size += len(data) receive_data += data else: print(receive_data.decode()) print("cmd res receive donr,,,,",size) client.close()
ftp server
1.读取文件名 2.检测文件是否存在 3.打开文件 4.检测文件大小 5.发送文件大小和md5给客户端
6.接受客户端确认信息(防止粘包) 7.开始边读边发
---恢复内容结束---
import socket,os import hashlib server = socket.socket() server.bind(('localhost',6969)) server.listen() while True: conn,addr = server.accept() print("new conn :",addr) while True: data = conn.recv(1024) if not data: print("客户端已经断开") break cmd,filename = data.decode().split() print(filename) if os.path.isfile(filename): f = open(filename,'rb') m = hashlib.md5() file_size = os.stat(filename).st_size # 获取文件大小 conn.send(str(file_size).encode('utf-8')) conn.recv(1024) # wait client ack for line in f: m.update(line) conn.send(line) print("file md5:",m.hexdigest()) f.close() conn.send(m.hexdigest().encode()) print("send done") server.close()
import socket import hashlib client = socket.socket() client.connect(('localhost',6969)) while True: cmd = input(">>:").strip() if len(cmd) == 0: continue if cmd.startswith("get"): client.send(cmd.encode('utf-8')) server_response = client.recv(1024) print("server_response:",server_response) client.send(b"ready to recv file") file_total_size = int(server_response.decode()) received_size = 0 filename = cmd.split()[1] f = open("new.docx","wb") m = hashlib.md5() while received_size < file_total_size: if file_total_size - received_size >= 1024: size = 1024 else: # 最后一次,剩下多少收多少 size = file_total_size - received_size print("--------",size) # 防止服务器两次send 粘包 data = client.recv(size) received_size += len(data) m.update(data) f.write(data) print(file_total_size,received_size) else: new_file_md5 = m.hexdigest() print("file recv done") f.close() server_file_md5 = client.recv(1024) print("server file md5:",server_file_md5) print("client file md5:",new_file_md5) client.close()
.