Home  >  Article  >  Backend Development  >  Python basic knowledge two: network communication data transmission

Python basic knowledge two: network communication data transmission

coldplay.xixi
coldplay.xixiforward
2021-02-20 09:43:392667browse

pythonImplementing network communication data transmission (basic knowledge (2))

Python basic knowledge two: network communication data transmission

Free learning recommendation: python video tutorial

  • ## Preface
  • Basic knowledge learning
    • 1. OSI model
    • 2. TCP, IP protocol family:
    • 3. Encoding method of strings in python
  • Conclusion

Preface

The second part is here! Let’s learn some basic knowledge today. A solid foundation is the only way to learn in depth! ! !

Basic knowledge learning

1. OSI model

  • OSI model divides network communication work into 7 layers, from bottom to top they are physical layer, data link layer, Network layer, transport layer, session layer, presentation layer and application layer.
    OSI is only a model that exists in concept and theory. Its shortcoming is that there are too many layers, which increases the complexity of network work, so it has not been applied on a large scale. Later, people simplified OSI and merged some layers. In the end, only 4 layers were retained. From bottom to top, they are interface layer, network layer, transport layer and application layer. This is the famous TCP/IP Model.
  • What exactly does this network model do? In short, it is data encapsulation.
  • Data encapsulation principle: The programs (software) we usually use generally access the network through the application layer, and the program generates The data will be transmitted down layer by layer until the final network interface layer is sent to the Internet through the network cable. Every time the data goes down a layer, it will be packaged by the protocol of this layer. By the time it is sent to the Internet, it has four more layers of packaging than the original data. The entire data encapsulation process is like a Russian matryoshka doll. When another computer receives the data packet, it will be transmitted layer by layer from the network interface layer. Each layer of transmission will be unpacked, until the final application layer, the most original data will be obtained. This is the data that the program will use. The process of packaging the data is actually adding a flag (a data block) to the head of the data, indicating that the data has passed through this layer and I have processed it. The process of unpacking data is exactly the opposite, which is to remove the mark in the header of the data and let it gradually reveal its original shape.
    Python basic knowledge two: network communication data transmission
    (Comparison between OSI seven-layer network model and TCP/IP four-layer network model)
  • When two computers communicate , must comply with the following principles:
    (1) Communication must be at the same level. For example, the application layer of computer A and the transport layer of computer B cannot communicate because they Not at the same level, data unpacking will encounter problems.
    (2) The functions of each layer must be the same, that is, they must have exactly the same network model. If the network models are all different, it will be a mess and no one will know anyone.
    Data can only be transmitted layer by layer, not layer-hopping.
    (3) Each layer can use the services provided by the lower layer and provide services to the upper layer.

2. TCP, IP protocol family:

The commonly used socket programming is Based on TCP and UDP protocols, their hierarchical relationship is shown in the following figure:
Python basic knowledge two: network communication data transmission

3. The encoding method of strings in python

#In Python, the encoding of the string must be converted to utf-8!

  • For the same number, you can choose different ways to represent it. Whichever solution you choose is a Unicode encoding method (utf-8 is a specific Unicode encoding method. accomplish).
  • Python uses Unicode internally to represent strings, so if you want to print any Chinese characters (Unicode and utf-8 encoding of English letters and numbers are the same), you need to It is guaranteed to be the Unicode encoding method. If it is the "utf-8" encoding method (the output is a bunch of binary codes, the UTF-8 encoding of a Chinese character is three bytes, that is, a three-segment binary representation, as shown below), you can Use .decode("utf-8") to decode. When saving to the hard disk or sending to the Internet (using sockets and other communication-related matters), it needs to be converted to UTF-8.
    Python basic knowledge two: network communication data transmission
    (Output result of utf-8 encoding in python)
  • Why not continue to use UTF-8 internally, otherwise there is no need Converted? This involves a shortcoming of UTF-8: calculating string length and finding substrings is very inefficient. When using UCS2, if you want to know how long the string is, just look at how many bytes it occupies, and then divide it by 2. For UTF-8, you need to count it character by character. . When doing substring search, because we don't know how many bytes the next character occupies, those efficient search algorithms will not work.

Conclusion

I will come up with some simple examples next time , to help everyone understand and practice it. Finally, I hope that everyone will give it a small like and give some encouragement after reading it. It has been more than two hours since I wrote this article. It is not an act of profit, but is just for knowledge sharing, learning together, and growing together! ! ! (Secretly, I don’t like other people’s articles after reading them. This is not a good habit. I will change it immediately, woo woo woo)

Related free learning recommendations: python tutorial(Video)

The above is the detailed content of Python basic knowledge two: network communication data transmission. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete