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

Python basic knowledge two: network communication data transmission

Feb 20, 2021 am 09:43 AM
python data transmission

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

See all articles