Python3之socket粘包问题

Posted 观山海

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python3之socket粘包问题相关的知识,希望对你有一定的参考价值。

解决方法一:

客户端:

from socket import *
import struct
import json

# ip = ‘127.0.0.1‘
ip=‘192.168.0.105‘
port = 8080
back_log = 5
buffer_size = 1024

tcp_client = socket(AF_INET, SOCK_STREAM)
tcp_client.connect((ip, port))

while True:
    cmd = input(‘>>: ‘).strip()
    if cmd is None: continue
    tcp_client.send(cmd.encode(‘utf-8‘))

    length = tcp_client.recv(buffer_size)
    length = int(length.decode(‘utf-8‘))

    tcp_client.send(b‘ready‘)

    recv_size = 0
    recv_msg = b‘‘

    while recv_size < length:
        recv_msg += tcp_client.recv(buffer_size)
        recv_size = len(recv_msg)

    cmd_res = recv_msg
    print(cmd_res.decode(‘gbk‘))

tcp_client.close()

  服务端:

from socket import *
import subprocess

ip = ‘‘
port = 8080
back_log = 5
buffer_size = 1024

tcp_server = socket(AF_INET, SOCK_STREAM)
tcp_server.bind((ip, port))
tcp_server.listen(back_log)

while True:
    conn, addr = tcp_server.accept()
    print(‘新的连接是‘, conn)
    print(‘新链接抵值‘, addr)

    while True:
        try:
            cmd = conn.recv(buffer_size)
            if not cmd: break
            res = subprocess.Popen(cmd.decode(‘utf-8‘), shell=True,
                                   stdout=subprocess.PIPE,
                                   stdin=subprocess.PIPE,
                                   stderr=subprocess.PIPE)

            err = res.stderr.read()
            if err:
                cmd_res = err
            else:
                cmd_res = res.stdout.read()

            length = len(cmd_res)
            conn.send(str(length).encode(‘gbk‘))
            ready = conn.recv(buffer_size)
            if ready == b‘ready‘:
                conn.send(cmd_res)

        except Exception as e:
            print(e)
            break
    conn.close()

tcp_server.close()

  

进阶版2:

  客户端:

技术分享图片
from socket import *
import struct
import json

ip = 127.0.0.1
# ip=‘192.168.0.105‘
port = 8080
back_log = 5
buffer_size = 1024

tcp_client = socket(AF_INET, SOCK_STREAM)
tcp_client.connect((ip, port))

while True:
    cmd = input(>>: ).strip()
    if cmd is None: continue
    tcp_client.send(cmd.encode(utf-8))

    length = tcp_client.recv(4)
    length = int(length.decode(gbk)[0])

    recv_size = 0
    recv_msg = b‘‘

    while recv_size < length:
        recv_msg += tcp_client.recv(buffer_size)
        recv_size = len(recv_msg)

    cmd_res = recv_msg
    print(cmd_res.decode(gbk))

tcp_client.close()
View Code

   服务端:

技术分享图片
from socket import *
import struct
import json

ip = 127.0.0.1
# ip=‘192.168.0.105‘
port = 8080
back_log = 5
buffer_size = 1024

tcp_client = socket(AF_INET, SOCK_STREAM)
tcp_client.connect((ip, port))

while True:
    cmd = input(>>: ).strip()
    if cmd is None: continue
    tcp_client.send(cmd.encode(utf-8))

    length = tcp_client.recv(4)
    length = int(length.decode(gbk)[0])

    recv_size = 0
    recv_msg = b‘‘

    while recv_size < length:
        recv_msg += tcp_client.recv(buffer_size)
        recv_size = len(recv_msg)

    cmd_res = recv_msg
    print(cmd_res.decode(gbk))

tcp_client.close()
View Code

 

进阶版3:

以上是关于Python3之socket粘包问题的主要内容,如果未能解决你的问题,请参考以下文章

python/socket编程之粘包

socket网络编程之粘包问题详解

python3 tcp的粘包现象和解决办法

python 之socket 粘包

python网络编程基础之socket粘包现象

python基础之socket编程part2---粘包和并发