python 之解決socket 粘包

2022-08-23 15:54:09 字數 4039 閱讀 3792

1、low版本(使用者端):

from socket import *ip_port = ("

127.0.0.1

",8080)

back_log = 5buffer_size = 1024tcp_client =socket(af_inet,sock_stream) #ipv4 internet協議,sock_stream位元組流,對應tcp,sock_dgram資料報文,對應udp

tcp_client.connect(ip_port)

while 1:

cmd = input("

>>>>>>>>:

").strip()

ifnot

cmd:

continue

if cmd == "

quit":

break

tcp_client.send(cmd.encode(

"utf-8"))

#解決粘包

length =tcp_client.recv(buffer_size)

tcp_client.send(b

"ready")

length = int(length.decode("

utf-8"))

recv_size =0

recv_msg = b""

while recv_size cmd_res =tcp_client.recv(buffer_size)

recv_msg = recv_msg +cmd_res

recv_size =len(recv_msg)

print("

命令的執行結果是

",recv_msg.decode("

gbk"

))tcp_client.close()

服務端:

from socket import *

import subprocess

ip_port = ("127.0.0.1",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 1:

conn,adr = tcp_server.accept()

print("新的客戶端連線是%s,位址是%s"%(conn,adr))

while 1:

try:

cmd = conn.recv(buffer_size)

if not cmd:

break

print("收到客戶端的命令",cmd)

res = subprocess.popen(cmd.decode("utf-8"),shell= true,stdout=subprocess.pipe,stderr=subprocess.pipe,stdin=subprocess.pipe)

err = res.stderr.read()

# if not err:#為空

if err:#有值

cmd = err

else:

cmd_res = res.stdout.read()#bite

if not cmd_res:

cmd_res = "執行成功".encode("gbk")

length = len(cmd_res)

conn.send(str(length).encode("utf-8"))

client_ready = conn.recv(buffer_size)

if client_ready == b"ready":

conn.send(cmd_res)

except exception as e:

print(e)

break

conn.close()

2、高逼格版本:(提公升效能)

使用者端:

from socket import *

import struct

ip_port = ("127.0.0.1",8080)

back_log = 5

buffer_size = 1024

tcp_client = socket(af_inet,sock_stream)

tcp_client.connect(ip_port)

while 1:

cmd = input(">>>>>>>>:").strip()

if not cmd:

continue

if cmd == "quit":

break

tcp_client.send(cmd.encode("utf-8"))

#解決粘包

length_data = tcp_client.recv(4)

length = struct.unpack("i",length_data)[0]

recv_size = 0

recv_msg = b""

while recv_size < length:

cmd_res = tcp_client.recv(buffer_size)

recv_msg = recv_msg + cmd_res

recv_size = len(recv_msg)

print("命令的執行結果是",recv_msg.decode("gbk"))

tcp_client.close()

服務端:

from socket import *

import

subprocess

import

struct

ip_port = ("

127.0.0.1

",8080)

back_log = 5buffer_size = 1024tcp_server =socket(af_inet,sock_stream)

tcp_server.bind(ip_port)

tcp_server.listen(back_log)

while 1:

conn,adr =tcp_server.accept()

print("

新的客戶端連線是%s,位址是%s

"%(conn,adr))

while 1:

try:

cmd =conn.recv(buffer_size)

ifnot

cmd:

break

print("

收到客戶端的命令

",cmd)

res = subprocess.popen(cmd.decode("

utf-8

"),shell= true,stdout=subprocess.pipe,stderr=subprocess.pipe,stdin=subprocess.pipe)

err =res.stderr.read()

#if not err:#為空

if err:#

有值 cmd =err

else

: cmd_res = res.stdout.read()#

bite

ifnot

cmd_res:

cmd_res = "

執行成功

".encode("

gbk"

) length =len(cmd_res)

data_length = struct.pack("i"

,length)

conn.send(data_length)

conn.send(cmd_res)

except

exception as e:

print

(e)

break

conn.close()

python入門之簡單socket

tcp from socket import host 127.0.0.1 port 12345 addr host,port server socket af inet,sock stream server.bind addr server.listen 5 print start listen....

Python之socket網路程式設計

實現主機間或一台電腦上的程序間進行通訊 例項如下 服務端 匯入socket sys模組 import socket import sys 建立socket物件 serversocket socket.socket socket.af inet,socket.sock stream 獲取本地主機名 h...

Python之Socket網路程式設計

socket起源於unix,而unix linux基本哲學之一就是 一切皆檔案 對於檔案用 開啟 讀寫 關閉 模式來操作。socket就是該模式的乙個實現,socket即是一種特殊的檔案,一些socket函式就是對其進行的操作 讀 寫io 開啟 關閉 socket和file的區別 1 file模組是...