search
HomeBackend DevelopmentPython TutorialPython basic knowledge two: network communication 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
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
What data types can be stored in a Python array?What data types can be stored in a Python array?Apr 27, 2025 am 12:11 AM

Pythonlistscanstoreanydatatype,arraymodulearraysstoreonetype,andNumPyarraysarefornumericalcomputations.1)Listsareversatilebutlessmemory-efficient.2)Arraymodulearraysarememory-efficientforhomogeneousdata.3)NumPyarraysareoptimizedforperformanceinscient

What happens if you try to store a value of the wrong data type in a Python array?What happens if you try to store a value of the wrong data type in a Python array?Apr 27, 2025 am 12:10 AM

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

Which is part of the Python standard library: lists or arrays?Which is part of the Python standard library: lists or arrays?Apr 27, 2025 am 12:03 AM

Pythonlistsarepartofthestandardlibrary,whilearraysarenot.Listsarebuilt-in,versatile,andusedforstoringcollections,whereasarraysareprovidedbythearraymoduleandlesscommonlyusedduetolimitedfunctionality.

What should you check if the script executes with the wrong Python version?What should you check if the script executes with the wrong Python version?Apr 27, 2025 am 12:01 AM

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

What are some common operations that can be performed on Python arrays?What are some common operations that can be performed on Python arrays?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

In what types of applications are NumPy arrays commonly used?In what types of applications are NumPy arrays commonly used?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

When would you choose to use an array over a list in Python?When would you choose to use an array over a list in Python?Apr 26, 2025 am 12:12 AM

Useanarray.arrayoveralistinPythonwhendealingwithhomogeneousdata,performance-criticalcode,orinterfacingwithCcode.1)HomogeneousData:Arrayssavememorywithtypedelements.2)Performance-CriticalCode:Arraysofferbetterperformancefornumericaloperations.3)Interf

Are all list operations supported by arrays, and vice versa? Why or why not?Are all list operations supported by arrays, and vice versa? Why or why not?Apr 26, 2025 am 12:05 AM

No,notalllistoperationsaresupportedbyarrays,andviceversa.1)Arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,whichimpactsperformance.2)Listsdonotguaranteeconstanttimecomplexityfordirectaccesslikearraysdo.

See all articles

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

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.