In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to customize pack and unpack in python". In daily operation, I believe many people have doubts about how to customize pack and unpack in python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to customize pack and unpack in python". Next, please follow the editor to study!
What is the sticky bag problem?
Because TCP protocol is a streaming protocol, there is no boundary between packets. Sometimes, due to operating system cache mechanism, network delay and other reasons, two short intervals and less data are merged into one transmission. Therefore, it affects the integrity of the data.
Sticky package problem solving strategy:
A common solution is to clarify the boundaries between packets through a custom protocol.
Sender:
1. Calculate the length before sending the packet, and then convert the int length data into a 4-byte bytes type
two。 Send 4 bytes of bytes length data first, and then send the packet.
Recipient:
1. First receive 4 bytes of bytes type length data and convert it into int type length data.
two。 Only data of a specified length is received.
The above protocol is a custom protocol that both the server and the terminal should abide by. In this way, the problem of sticky bags can be solved.
Distinguish between messages and files
Generally speaking, the message size is set to 1024 bytes, and the length of the message should be sent before sending the message. When receiving, you also have to receive the length first, and then receive the message.
When sending a file, first calculate the size of the file, and then send the length of the file, and then send the file. When receiving, you should first receive the file size, and the socket file transceiver cache size is usually set to 65535. Note: the size of the packet sent by the sender is fixed when the file is sent, but the size of the packet received by the receiver is not fixed each time. After each receipt, the length of the file minus the length of the received packet should be used to determine that the length of the remaining unreceived file is less than 1.
Stuct module pack and unpack defects
To deal with the sticky package problem, I looked up a lot of materials and saw that most people are import struct, using struct.pack and unpack to complete the conversion between int data and bytes. But I think struct module pack and unpack have two defects: one is that the range of values is-2147483648 to 2147483647, negative values are completely useless in calculating the data length, which will cause the upload and download file size to be no more than 2 GGrainUnpack returns a tuple, and you have to unpack the tuple before you can use it. So I tried to write a pack and unpack to share with you below.
Custom pack and unpackdef pack (n): if n > = 4294967296 or n
< 0:raise ValueError('The value is out of range.')values = ((0b11111111000000000000000000000000, 24), (0b111111110000000000000000, 16), (0b1111111100000000, 8), (0b11111111, 0))ret = b''for i in values:x = (n & i[0]) >> I [1] x = x.to_bytes (length=1, byteorder= "big") ret + = xreturn retdef unpack (numlist): if len (numlist)! = 4:raise ValueError ("The bytes length must be 4.") values = (24,16,8,0) ret = 0i = 0while I < 4MULN = numList [I] ret + = n
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.