Python learning diary (31) sticky bag issue

import subprocess

res = subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
print('Stdout:',res.stdout.read().decode('gbk'))
print('Stderr:',res.stderr.read().decode('gbk'))

PIPE things attached to output a 'tube', the windows if the encoding format is GBK, the execution result:

Stdout: drive C in the system volume is 
 the volume serial number is 85C0 - 669A 

 C: \ the Users \ Administrator \ PycharmProjects \ Internet_program directory

 2019/09/16 13:48 <the DIR>           .
 2019/09/16 13:48 < the DIR>           ..
 2019/09/16 13:47 <the DIR>           .idea
 2019/09/16 13:46 21 is Client1.py
 2019/09/16 13:42                  0 Client2.py
 2019/09/16 13:48 207 Sever1.py
 2019/09/16 01:41 70 time_test.py
 2019/09/14 23:51 <the DIR>           Venv
                four files 298 bytes
                4 directory 45,863,636,992 bytes available

Stderr: 

Here you can also use os.popen () but it will result regardless of right and wrong are put together, but with subprocess can get the correct and incorrect information respectively

Sticky TCP packet-based implementation

Sever:

import socket
sk = socket.socket()
sk.bind(('127.0.0.1',8092))
sk.listen()
conn,addr = sk.accept()
while True:
    cmd = input('<<<')
    conn.send(cmd.encode('gbk'))
    ret = conn.recv(1024).decode('gbk')
    print(ret)
conn.close()
sk.close()

Client:

import socket
import subprocess
sk = socket.socket()
sk.connect(('127.0.0.1',8092))
while True:
    cmd = sk.recv(1024).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('gbk'))
    sk.send(std_err.encode('gbk'))
sk.close()

Results of the:

Sever:

<<< the dir; LS 
stdout: drive C is the system volume 
 Volume Serial Number is 85C0 - 669A 

 C: \ the Users \ Administrator \ PycharmProjects \ Internet_program directory


 <<< the ipconfig 
stderr: File not found

 <<<

Client:

stdout: Volume in drive C is the system 
 Volume Serial Number is 85C0 - 669A 

 C: \ the Users \ Administrator \ PycharmProjects \ Internet_program directory 


stderr: File not found 

stdout: 
Windows IP Configuration 


Ethernet adapter Bluetooth network connection 2 : 

   media state ............: media disconnected 
   connection-specific DNS suffixes:....... 

Ethernet adapter local Area connection: 

   media state............ : media disconnected 
   connection-specific DNS suffix:....... 

wireless LAN adapter wireless network connection: 

   connection-specific DNS suffix:....... 
   link-local IPv6 address........ : fe80 :: 8c6: 36a9: 6fa6: 8018% 14 
   IPv4 address:............ 192.168.43.216 
   subnet mask:............ 255.255. 255.0
   Default Gateway:............. 192.168.43.1 

Tunnel adapter Local Area Connection * 3 : 

   Media State:........... Media disconnected 
   Connection-specific DNS Suffix .......: 

stderr:

When we enter dir at the sever end; when ls command, only the results to stdout ran out, and when we enter ipconfig command, the system will last dir; the results have not been performed in the stderr ls to run out. Like this is not fully accepted or accepted much of the package is sticky phenomenon.

TCP will stick pack phenomenon but it is not dropping.

Based on sticky UDP packet to achieve

Sever:

import socket
sk = socket.socket(type=socket.SOCK_DGRAM)
sk.bind(('127.0.0.1',8092))
msg,addr = sk.recvfrom(10240)

while 1:
    cmd = input('<<<')
    if cmd == 'q':
        break
    sk.sendto(cmd.encode('gbk'),addr)
    msg,addr = sk.recvfrom(10240)
    print(msg.decode('gbk'))

sk.close()

Client:

import socket
import subprocess
sk = socket.socket(type=socket.SOCK_DGRAM)
addr = ('127.0.0.1',8092)
sk.sendto('Start'.encode('utf-8'),addr)
while 1:
    cmd,addr = sk.recvfrom(10240)
    ret = subprocess.Popen(cmd.decode('gbk'),shell=True,stderr=subprocess.PIPE,stdout=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('gbk'),addr)
    sk.sendto(std_err.encode('gbk'),addr)
sk.close()

Results of the:

Sever:

<<< the dir; LS 
the Stdout: drive C is the system volume 
 Volume Serial Number is 85C0 - 669A 

 C: \ the Users \ Administrator \ PycharmProjects \ Internet_program directory


 <<< the dir 
the Stderr: File not found

 <<< the ipconfig 
the Stdout : C in the drive system volume is 
 the volume serial number is 85C0 - 669A 

 C: \ the Users \ Administrator \ PycharmProjects \ Internet_program directory

 2019/09/16 14:43 <the DIR>           .
 2019/09/16 14:43 <the DIR >           ..
 2019/09/16 14:37 <the DIR>           .idea
 2019/09/16 14:43 553 Client1.py
 2019/09/16 13:42                  0 Client2.py
 2019/09/16 14:43 306Sever1.py
 2019/09/16 01:41 70 time_test.py
 2019/09/14 23:51 <the DIR>           Venv
                four files 929 bytes
                4 directory 45,855,449,088 bytes available

 <<< pwd 
the Stderr:
 <<< IP 
stdout: 
Windows IP configuration 


Ethernet adapter Bluetooth network connection 2 : 

   media state:........... media disconnected 
   connection-specific DNS suffix:....... 

Ethernet adapter local Area connection : 

   media state:........... media disconnected 
   connection-specific DNS suffix:....... 

wireless LAN adapter wireless network connection:
 
   connection-specific DNS suffixes..... .:
   IPv6 link-local address:........ Fe80 :: 8c6: 36a9: 6fa6:  8018 14%
   IPv4 address:............ 192.168.43.216 
   subnet mask...... ......: 255.255.255.0 
   default gateway:............. 192.168.43.1 

tunnel adapter local Area connection * 3 : 

   media state........... : media disconnected 
   connection-specific DNS suffixes:.......

 <<<

Client:

Stdout: drive C in the volume is the system 
 volume serial number is 85C0 - 669A 

 C: \ the Users \ Administrator \ PycharmProjects \ Internet_program directory 


Stderr: File not found 

Stdout: drive C in the volume is the system 
 volume serial number is 85C0 - 669A 

 C: \ the Users \ Administrator \ PycharmProjects \ Internet_program directory

 2019/09/16 14:43 <the DIR>           .
 2019/09/16 14:43 <the DIR>           ..
 2019/09/16 14:37 <the DIR>           .idea
 2019/09/16 14:43 553 Client1.py
 2019/09/16 13:42                  0 Client2.py
 2019/09/16 14:43 306 Sever1.py
 2019/09/16 01:41 70 time_test.py
23:51 2019/09/14 <the DIR>           Venv
                four files 929 bytes
                4 directory 45,855,449,088 bytes available 

the Stderr: 
the Stdout: 
the Windows configure the IP 


Ethernet adapter Bluetooth network connection 2 : 

   media state........ ....: media disconnected 
   connection-specific DNS suffixes:....... 

Ethernet adapter local Area connection: 

   media state:............ media disconnected 
   connection-specific DNS suffix:....... 

wireless LAN adapter wireless network connection: 

   connection-specific DNS suffix:....... 
   link-local IPv6 address:....... . fe80 :: 8c6: 36a9: 6fa6: 8018% 14 
   IPv4 address:............ 192.168.43.216 
   subnet mask............:255.255.255.0 
   Default Gateway:............. 192.168.43.1 

Tunnel adapter Local Area Connection * 3 : 

   Media State:........... Media disconnected 
   connection-specific the DNS suffix:....... 

Stderr: 
Stdout: 
Stderr: ' pwd ' is not an internal or external command, operable program 
or batch file. 

Stdout: 
Stderr: ' ip ' is not an internal or external command, operable program 
or batch file.

UDP can see that there will be no sticky bag phenomenon, will produce packet loss, not finished not made, incomplete and unreliable.

Sticky bag causes

TCP data transfer protocol

Unpacking mechanism

When the length of the transmission side buffer is larger than the MTU of the network card, the data will be transmitted TCP split into several data packets sent. Maximum Transmission Unit MTU is the acronym, meaning the maximum packet transfer network, MTU is in units of bytes, the majority of network devices are MTU 1500. If the MTU is larger than the MTU native gateway, the data packets will be large is disassembled to transmit, it will generate a lot of packet fragmentation, loss rate increases, reducing the speed of the network.

Under normal circumstances it is understandable unpacking:

Nagle algorithm for communication and flow characteristics

TCP (transport control protocol, the Transmission Control Protocol) is connection-oriented and stream-oriented, provide a highly reliable service. The receiver and transmitter (client and server) must have eleven socket pairs, so in order to transmit end a plurality of packets destined to the reception side, more effectively sent to the other, using an optimization algorithm (the Nagle algorithm), the repeatedly a small distance and a small amount of data the data combined into a large block of data, then the packet. In this way the receiving end it is hard to tell the difference, and must provide scientific unpacking mechanism. I.e., non-oriented communication message stream is protected boundaries.

For an empty message: TCP is a stream-based, so messaging can not be empty, which requires both the client and server to add an empty message handling mechanism to prevent jamming program, and the UDP protocol is datagram based, even you entered is empty content (directly enter), may also be transmitted, UDP protocol will help you message is then sent to the package.

Reliable sticky package TCP protocols: TCP protocol data will not be lost, not finished closed pack, the next will be receiving, we will continue to continue to receive the last.

Based on the characteristics of the causes tcp protocol packet sticky phenomenon

When we in the socket server sends the value 1, then according to the optimization algorithm, it will put a first cache among the wait, and then packaged with up 2, and then sent out, so we see that sticky phenomenon package

Surface phenomena of this phenomenon is too close and the two send short messages sent

1K1K transmitting end can transmit data, and the receiving end two applications can be put away K K two data, of course, also possible to withdraw a 3K or 6K data, or withdraw only a few bytes of data. That is, the application data is seen as a whole, or is a stream (Stream), a program corresponding to the message how many bytes is not visible, so the TCP protocol is a protocol for streaming, it is prone to the reason sticky bag problem. Message-oriented protocol is the UDP protocol, each segment is a UDP message, the application must extract the data units of the message can not be extracted any one-byte data, and it is different from TCP.

 

Sticky solution package

 

...

Guess you like

Origin www.cnblogs.com/Fantac/p/11527758.html