In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
First, learn about the development of coding.
1. Computers can only process numbers. If you need to process text, you need to convert text to numbers first. Because computers were invented by Bangzi in the United States, they invented the earliest coding-ASCII coding, which encodes their uppercase and lowercase characters, numbers and some symbols into the computer. For example, the code of An is 65.
2. With the development of computers in China, it is obviously not enough to use ASCII to process Chinese, because English needs one byte, while Chinese needs two bytes. In order to meet the demand, China has invented its own coding GB2312. Put Chinese into it.
3. As is the case in China, do not every language in the world need a coding standard? if there are multiple languages in a text, the result will be garbled.
4. In order to adapt to the development, the Unicode code was born, which unifies all languages into one set of codes. In this way, there will be no more garbled problems. Although this thing is powerful enough, it has a disadvantage to be reckoned with. It has twice as much storage space as ASCII coding. For example: an ascii:65 binary: 01000001 unicode:00000000 01000001
5. Therefore, adhering to the principle of economy, UTF-8 arises at the historic moment, and the benefit code can be longer. For example, the UTF-8:01000001 of A can save the previous zeros in unicode coding.
2. The working mode of general coding in computer systems
1. When we edit the document, we read the contents of the document and convert UTF-8 characters to unicode characters into memory. Because it needs to be shown here, to avoid garbled code, use the general Unicode coding.
2. When the coding editing is finished, convert the Unicode encoding to UTF-8 and save it to the file. Because it needs to be stored in disk here, UTF-8 encoding with variable coding length is used to save storage space.
III. Python3 character coding
The string type of python is str and is represented as Unicode in memory.
1. If you need to transfer it over the network, or save it to a storage device, you need to change the str into a bytes in bytes. You can use the encode () method. Through the understanding of the first major point, we know that the str represented by Unicode can be transformed by ASCII and utf-8 coding. However, when converting Chinese, be sure to use utf-8, because the str containing Chinese cannot be encoded in ASCII, which is beyond the scope of ASCII coding. For example:
2. If we read data from the network or disk, we will change bytes into str. You can use the decode () method.
From the above understanding, we can see that there is no error in utf-8 encoding, whether reading data or transmitting data, so utf-8 is more widely used in many cases.
Fourth, analyze the problems I actually encounter.
Use the socket module to write a script for communication, the original server:
#! / usr/bin/python3import sockets=socket.socket (socket.AF_INET,socket.SOCK_STREAM) s.bind ((', 6000)) s.listen (1) client,addr=s.accept () print ("Connected by", addr) client.send ("welcome") text=client.recv (1024) print (text) client.close () s.close ()
Original client:
#! / usr/bin/python3import socketc=socket.socket (socket.AF_INET,socket.SOCK_STREAM) c.connect (("192.168.80.128", 6000)) ans=c.recv (1024) print (ans) c.send ("hello") c.close ()
Execution result
The send () method is used to pass the data, passing the hello to the server.
The error indicates that a string of type str needs to be converted to bytes. When we pass data, we should first convert str to data of type bytes. Use encode ('utf-8') or encode (' ascii'); at the same time, the data passed by the server's send () method should also be converted.
The server modifies the code:
Client.send ("welcome" .encode ('utf-8'))
Client modifies the code:
C.send ("hello" .encode ('utf-8'))
The result of the modified execution:
The execution was successful.
During execution, the str is converted to bytes and passed to the other party. Because what is passed here is a letter, there is no bytes on the content, but there is a letter b before the string, which means bytes. If it is delivered in Chinese:
Because we converted str to bytes. So when we accept data, we should convert bytes to str
The server modifies the code:
Text=client.recv (1024). Decode ('utf-8')
Client modifies the code:
Ans=c.recv (1024). Decode ('utf-8')
Execution result:
The summary is over!
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.