一、TCP
tcp中遇到黏包会让数据传输完。
server
import socket sk=socket.socket() sk.bind(('127.0.0.1',8090)) sk.listen() while True: cmd=input('>>>') conn,addr=sk.accept() conn.send(cmd.encode('utf-8')) ret=conn.recv(1024).decode('utf-8') print(ret) conn.close() sk.close()
client
import socket import subprocess sk=socket.socket() sk.connect(('127.0.0.1',8090)) while True: cmd=sk.recv(1024).decode('gbk') ret=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) std_out='stdout:'+(ret.stdout.read()).decode('gbk') std_err='stderr:'+(ret.stderr.read()).decode('gbk') print(std_err) print(std_out) sk.send(std_out.encode('utf-8')) sk.send(std_err.encode('utf-8')) sk.close()
二、UDP
udp传输遇不会黏包。
server
import socket sk=socket.socket(type=socket.SOCK_DGRAM) sk.bind(('127.0.0.1',8090)) msg,addr=sk.recvfrom(1024) while True: cmd=input('>>>') if cmd=='q':break sk.sendto(cmd.encode('utf-8'),addr) msg,addr=sk.recvfrom(1024) print(msg.decode('utf-8')) sk.close()
client
import socket import subprocess sk=socket.socket(type=socket.SOCK_DGRAM) addr=('127.0.0.1',8090) sk.sendto('吃了吗'.encode('utf-8'),addr) while True: cmd,addr=sk.recvfrom(1024) ret=subprocess.Popen(cmd.decode('gbk'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) std_out='stdout:'+(ret.stdout.read()).decode('gbk') std_err='stderr:'+(ret.stderr.read()).decode('gbk') print(std_err) print(std_out) sk.sendto(std_out.encode('utf-8'),addr) sk.sendto(std_err.encode('utf-8'),addr) sk.close()
三、黏包
tcp协议的拆包机制:网络设备在传输时会把数据拆成几块来传输,这样会产生数据碎片,从而造成数据丢失。
tcp黏包:发送方引起的粘包是由TCP协议本身造成的,TCP为提高传输效率,发送方往往要收集到足够多的数据后才发送一个TCP段。若连续几次需要send的数据都很少,通常TCP会根据优化算法把这些数据合成一个TCP段后一次发送出去,这样接收方就收到了粘包数据。
四、网盘作业
server
import socket sk=socket.socket() sk.bind(('127.0.0.1',8080)) sk.listen() conn,addr=sk.accept() user=conn.recv(1024).decode('utf-8') pwd=conn.recv(1024).decode('utf-8') print(user,pwd) while True: if user=='alex' and pwd=='123456': conn.send(bytes('1.上传文件 2.下载文件',encoding='utf-8')) shell=conn.recv(10240).decode('utf-8') print(shell) if shell== '1': txt=conn.recv(10240).decode('utf-8') with open('a.txt','w',encoding='utf-8') as f1: f1.write(txt) conn.send(bytes('上传成功',encoding='utf-8')) break elif shell== '2': with open('b.txt','r',encoding='utf-8') as f1: conn.send(bytes(f1.read(), encoding='utf-8')) conn.send(bytes('下载成功',encoding='utf-8')) break conn.close() sk.close()
client
import socket sk=socket.socket() sk.connect(('127.0.0.1',8080)) user=input('输入用户名:') pwd=input('输入密码:') sk.send(bytes(user,encoding='utf-8')) sk.send(bytes(pwd,encoding='utf-8')) shell=sk.recv(1024).decode('utf-8') print(shell) cmd=input('>>>') sk.send(bytes(cmd,encoding='utf-8')) while True: if cmd=='1': with open('a1.txt','r',encoding='utf-8') as f1: sk.send(bytes(f1.read(),encoding='utf-8')) msg=sk.recv(10240).decode('utf-8') print(msg) break elif cmd=='2': with open('C:/Users/lenovo/Desktop/111/b1.txt','w',encoding='utf-8') as f1: info=sk.recv(10240).decode('utf-8') msg=sk.recv(10240).decode('utf-8') f1.write(info) print(msg) break sk.close()