2021-1-18网络编程解决粘包问题,与基于udp协议的网络编程

客户端粘包版:

import socket
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(('127.0.0.1',8090))

while True:
    msg = input(">>>:").strip()
    if len(msg) == 0:
        continue
    client.send(msg.encode('utf-8'))
    data = client.recv(1024)
    print(data.decode('gbk'))

服务端粘包版:

import socket
import subprocess
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(('127.0.0.1',8090))
server.listen(5)
while True:
    conn,client_addr=server.accept()
    print(conn)
    while True:
        try:
            data = conn.recv(1024)
            cmd = data.decode('utf-8')
            obj = subprocess.Popen(cmd,shell=True,
                                   stdout = subprocess.PIPE,
                                   stderr = subprocess.PIPE)
            res1 = obj.stdout.read()
            res2 = obj.stderr.read()
            conn.send(res1+res2)
        except Exception:
            break

    conn.close()
server.close()

tcp 协议客户端升级版:

from socket import *
import struct
import json
#买手机
client = socket(AF_INET,SOCK_STREAM)
#打电话
client.connect(('127.0.0.1',9090))
#收发数据
while True:
    cmd = input('>>>:').strip()
    if len(cmd) == 0:
        continue
    client.send(cmd.encode('utf-8'))


    header_bytes_len = struct.unpack('i',client.recv(4))[0]
    print(header_bytes_len)

    #再收header_bytes,提取header_dic
    header_bytes = client.recv(header_bytes_len)
    header_json = header_bytes.decode('utf-8')
    header_dic = json.loads(header_json)
    print(header_dic)
    total_size = header_dic["total_size"]
    # 在接收真正的数据
    recv_size = 0
    res =b''
    while recv_size < total_size:
        data = client.recv(1024)
        recv_size += len(data)
        res += data


    print(res.decode('gbk'))

tcp 协议服务端升级版:


from socket import *
from subprocess import Popen,PIPE
import struct
import json







server = socket(AF_INET,SOCK_STREAM)
server.bind(('127.0.0.1',9090))
server.listen(5)
# 链接循环
while True:
    conn,client_addr = server.accept()
    print(client_addr)

# 通信循环
    while True:
        try:
            cmd = conn.recv(8096)
            if cmd ==0:
                break
            obj = Popen(cmd.decode('utf-8'),
                        shell = True,
                        stderr=PIPE,
                        stdout=PIPE
                        )
            res1 = obj.stdout.read()
            res2 = obj.stderr.read()
            header_dict ={
     
                'filename':"a.txt",
                'total_size':len(res1)+len(res2),
                'md5':"hdkldhs"
            }
            header_json = json.dumps(header_dict)
            header_bytes = header_json.encode('utf-8')
            # 先发4个字节
            conn.send(struct.pack('i',len(header_bytes)))
            # 再发报头字典
            conn.send(header_bytes)
            # 最后发送真正的数据
            conn.send(res1)
            conn.send(res2)
        except Exception:
            break

    conn.close()

基于udp协议运行的程序
客户端:

import socket
client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

while True:
    msg = input('>>>:').strip()
    client.sendto(msg.encode('utf-8'),('127.0.0.1',8001))
    res,server_addr = client.recvfrom(1024)
    print(res.decode('utf-8'))

服务端:

import socket
server = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
server.bind(('127.0.0.1',8001))
while True:
    data,client_addr = server.recvfrom(1024)
    print(data,client_addr)
    server.sendto(data.upper(),client_addr)

你可能感兴趣的:(python基础课程)