python--Sticky Bag

Source: Internet
Author: User

Sticky bag phenomenon

A program (command Ls-l) that first makes a remote execution of commands based on TCP; lllllll; Pwd

Res=subprocess. Popen (Cmd.decode ('utf-8'), Shell=True,stderr=subprocess. Pipe,stdout=subprocess. PIPE) The result of the encoding is based on the current system, if it is windows, then Res.stdout.read () read out is GBK encoded, at the receiving end needs to use GBK decoding and can only read the results from the pipeline

After executing multiple commands at the same time, the resulting results are likely to be only part of the result, and when other commands are executed, another part of the result of the previous execution is received, which is the sticky packet.

The implementation of sticky packets based on TCP protocol
ImportSocketImportSubprocesssk=socket.socket () Sk.connect ('127.0.0.1', 8090)) whileTrue:cmd= Sk.recv (1024x768). 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_out)Print(Std_err) sk.send (Std_out.encode ('Utf-8')) Sk.send (Std_err.encode ('Utf-8') ) Sk.close ()
ImportSocketsk=socket.socket () Sk.bind ('127.0.0.1', 8090) ) Sk.listen () conn,addr=sk.accept () whileTrue:cmd= Input ('>>>') Conn.send (Cmd.encode ('Utf-8')) RET= Conn.recv (1024x768). Decode ('Utf-8')    Print(ret) conn.close () sk.close ()
The implementation of sticky packets based on UDP protocol
ImportSocketImportSubprocesssk= Socket.socket (type=socket. SOCK_DGRAM) Addr= ('127.0.0.1', 8090) Sk.sendto ('did you eat?'. Encode ('Utf-8'), addr) whiletrue:cmd,addr= Sk.recvfrom (10000) 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_out)Print(Std_err) sk.sendto (Std_out.encode ('Utf-8'), addr) sk.sendto (Std_err.encode ('Utf-8'), addr) sk.close ()
ImportSocketsk= Socket.socket (type=socket. SOCK_DGRAM) Sk.bind (('127.0.0.1', 8090)) Msg,addr= Sk.recvfrom (10240) whileTrue:cmd= Input ('>>>')    ifcmd = ='Q':         Breaksk.sendto (Cmd.encode ('Utf-8'), addr) msg,addr= Sk.recvfrom (10240)    Print(Msg.decode ('Utf-8') ) Sk.close ()#UDP#UDP does not adhere to packets#UDP will drop packets

Note: Only TCP has sticky packet behavior, UDP never sticky

the cause of sticky packet phenomenon based on TCP protocol characteristics

For example, the TCP-based socket client to the server to upload files, the content of the file is sent in accordance with a paragraph of the stream of bytes sent, in the receiver looked at, do not know where the file's byte stream from where to start, where to end

In addition, the packet caused by the sender is caused by the TCP protocol itself, TCP to improve transmission efficiency, the sender often to collect enough data before sending a TCP segment. If there are few data to send in a few consecutive times, TCP will usually send the data to a TCP segment based on the optimization algorithm, and the receiver receives the sticky packet data.

UDP does not occur sticky packets

UDP (User Datagram Protocol, Subscriber Datagram Protocol) is non-connected, message-oriented, providing efficient service. The Block merging optimization algorithm is not used, because UDP supports a one-to-many pattern, so the receiver Skbuff (socket buffer) uses a chain structure to record each incoming UDP packet, in each UDP packet there is a message header (message source address, port and other information), so for the receiving end , it is easy to distinguish between the processing. That is, message-oriented communication is a message-protected boundary. For the empty message: TCP is based on data flow, so send and receive messages can not be empty, which requires the client and the server to add a null message processing mechanism, to prevent the program stuck, and UDP is based on the datagram, even if you enter the empty content (direct carriage), can also be sent, The UDP protocol will help you encapsulate the message hair sent over. Unreliable non-sticky UDP protocol: UDP recvfrom is blocked, a recvfrom (x) must be the only one sendinto (y), the data after the X-byte is completed, if the y;x data is lost, which means that UDP is not sticky packets, but will lose data, Not reliable.

python--Sticky Bag

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.