-socket Programming of Python basics

Source: Internet
Author: User

Sticky bag phenomenon

Let's make a program that executes commands remotely based on TCP (1: Execute Error command 2: Execute LS 3: Execute ifconfig)

Note Note:

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 need to use GBK decoding

And can only read one result from the pipe.

Note: Command ls-l; lllllll; The results of PWD are both correct stdout results and error stderr results

#!/usr/bin/python3#_*_coding:utf-8_*_#__author__ = ' Tengbibo 'ImportSocketImportSubprocessip_port= ('127.0.0.1', 6969) BUFSIZE= 1024Tcp_socket_server=Socket.socket () tcp_socket_server.bind (Ip_port) Tcp_socket_server.listen (5) whiletrue:conn,addr=tcp_socket_server.accept ()Print('Connect from:', addr) whileTrue:cmd=conn.recv (BUFSIZE)ifLen (cmd) = = 0: BreakRes= subprocess. Popen (Cmd.decode ('Utf-8'), Shell =True, stdout=subprocess. PIPE, stdin=subprocess. PIPE, stderr=subprocess. PIPE) stderr=res.stderr.read () stdout=Res.stdout.read () conn.send (stderr) conn.send (stdout)
Service Side
#!/usr/bin/python3#_*_coding:utf-8_*_#__author__ = ' Tengbibo 'Importsocketbufsize=1024Ip_port=('127.0.0.1', 6969) Client=Socket.socket () client.connect (Ip_port) whileTrue:cmd= Input ('>>:'). Strip ()ifLen (cmd) = = 0:Continue    ifcmd = ='quit': Breakclient.send (Cmd.encode ('Utf-8')) Act_res=client.recv (BUFSIZE)Print(Act_res.decode ('Utf-8'), end="')
Client

The above program is a TCP-based socket that will occur at runtime when sticky packets

What is a sticky bag

Note: Only TCP has a sticky packet phenomenon, UDP will never sticky packets.

First you need to master the principle of a Socket transceiver message

                  

The sender can be a K-K to send the data, and the receiving side of the application can be two K two k to take the data, of course, it is possible to take 3 K or 6K data at a time, or only a few bytes of data at a time, that is, the application sees the data is a whole, or a stream (stream), The number of bytes of a message is not visible to the application, so the TCP protocol is a stream-oriented protocol, which is also the cause of the sticky packet problem. And UDP is a message-oriented protocol, each UDP segment is a message, the application must be in the message to extract data, not one time to extract arbitrary bytes of data, which is very different from TCP. How do you define a message? Can think of the other one-time Write/send data for a message, it is necessary to understand that when the other side send a message, regardless of the underlying how fragmented shards, the TCP protocol layer will constitute the entire message of the data segment is completed before rendering in the kernel buffer.

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

The so-called sticky packet problem is mainly because the receiver does not know the boundary between the message, do not know how many bytes of data extracted at once.

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.

    1. TCP (Transport Control Protocol, transmission Protocol) is connection-oriented, stream-oriented and provides high reliability services. Both ends of the transceiver (client and server side) have one by one pairs of sockets, so the sending side in order to send multiple packets to the receiver, more efficient to the other side, the use of the optimization method (Nagle algorithm), the multiple interval small and small data volume data, combined into a large block of data, and then to the packet. In this way, the receiving end, it is difficult to distinguish out, must provide a scientific unpacking mechanism. That is, stream-oriented communication is a non-message-protected boundary.
    2. 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.
    3. 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), it is not an empty message, the UDP protocol will help you encapsulate the message header, the experiment slightly

UDP Recvfrom is blocked, a recvfrom (x) must be the only one sendinto (y), after the X-byte data is completed, if the y>x data is lost, which means that UDP is not sticky packets, but will lose data, unreliable

TCP protocol data is not lost, no packets are received, the next time it is received, it continues to receive the last time, and the buffer content is always cleared when the ACK is received by the client. The data is reliable, but it will stick to the package.

-socket Programming of Python basics

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.