Python Network Programming -- network basics

Yongliang 2020-11-12 13:44:14
python network programming network basics


1、 The basic concept of the network

(1)、 Network architecture

C / S framework : client client / server Server side

Such as :qq WeChat browser Hero alliance Crossing the line of fire ··· --- The client needs to be installed

The client here generally refers to the client application EXE, The program needs to be installed before , To run on the user's computer , It depends on the user's computer operating system environment .

img

B / S framework : browser browser / server The server

Such as : Baidu TaoBao Code cloud ··· --- Just enter the URL in the browser and you can use it directly

B / S Browser in Architecture , It's also a kind of client , But you don't need to install any application , Just go through HTTP Request server-side related resources ( Web resources ), The client browser can add, delete, modify and check .

img

B / S yes C / S One of the architectures ;C / S Architecture security is high , But the program is huge .

(2)、 Mobile

APP

Wechat applet : Unifies all web Program entry

Alipay : Unifies all the money related things

(3)、mac Address

Mac An address is a physical address , It uniquely identifies a network device , Each network card is burned to be the only one in the world when it leaves the factory mac Address , The length is 48 position 2 Base number , Usually by 12 position 16 A decimal number means ( The first six digits are the manufacturer's number , The last six digits are the assembly line number )

head The source and destination addresses contained in are from :ethernet Provision for access internet All devices must have network cards , The address of sender and receiver refers to the address of network card , namely mac Address .

(4)、IP Address

IP Address is a logical address , It can change according to the position , Be able to locate quickly in Wan

<1>、IPV4 Address

Express : 4 Decimals

Range : 0.0.0.0 - 255.255.255.255

Number :2**32

<2>、IPV6 Address

6 Bits are hexadecimal

0.0.0.0 - FFFF:FFFF:FFFF:FFFF:FFFF:FFFFF

(5)、 Public and Intranet

Public network : 0.0.0.0 - 255.255.255.255 ( Without reserved fields IP)

Be able to visit anywhere IP Address

Intranet : All the intranet IP Use reserved fields

It can only be used in one area , You can't use it out of this area

# ip Reserved address
a Class network :192.168.0.0 - 192.168.255.255
b Class network :172.16.0.0 - 172.32.255.255
c Class network :10.0.0.0 - 10.255.255.255

(6)、 Routers and switches

<1>、 Switch

Switch (Switch) It's used for electricity ( light ) Signal forwarding [ Network devices . It can provide exclusive electrical signal path for any two network nodes of access switch . The most common switch is the Ethernet switch . Other common ones are telephone voice switches 、 Optical switch, etc .

# The switch completes LAN communication
The switch doesn't know IP Address , Know only mac Address
adopt IP look for mac Address :arp agreement ( Address resolution protocol )
The way : unicast radio broadcast Multicast
<2>、 Router

Two or more devices are connected to a network , Act as a gateway between networks , It's a dedicated intelligent network device that reads the address in each packet and decides how to transmit it .

# The router completes the communication between LANs

img

(7)、 gateway IP

Gateway is essentially a network to other networks IP Address .

(8)、 Subnet mask

Subnet mask , It's a parameter that represents the characteristics of a subnet . It is formally equivalent to IP Address , Also a 32 Bit binary number , Its network part is 1, The main part is 0. such as ,IP Address 172.16.10.1, If the network part is known to be pre 24 position , The main part is the rear 8 position , So the subnet mask is 11111111.11111111.11111111.00000000, It's written in decimal 255.255.255.0.

know ” Subnet mask ”, You can judge , Any two IP Is the address on the same subnet . The way is to put two IP Address and subnet mask respectively AND operation ( Both digits are 1, The result of operation is 1, Otherwise 0), Then compare the results to see if they are the same , If so , It means they are in the same subnet , Otherwise it's not .

255.0.0.0

255.255.0.0

255.255.255.0

# Whether two IP Whether the address is in the same network segment If the result is the same, it is the same segment , On the contrary, it is not , IP and Subnet mask Between Bitwise operation
192.168.13.26
255.255.0.0
11000000.10101000.00001101.00011010
11111111.11111111.00000000.00000000
11000000.10101000.00000000.00000000 = 192.168.0.0
192.168.12.134
255.255.0.0
11000000.10101000.00001100.10000110
11111111.11111111.00000000.00000000
11000000.10101000.00000000.00000000 = 192.168.0.0
# The result is 192.168.0.0, So they're on the same subnet .

(9)、port port : 0 - 65535

​ One has IP The host of the address can provide many services , such as Web service 、FTP service 、SMTP Service etc. , These services are fully accessible through 1 individual IP Address to achieve . that , How do hosts differentiate between different network services ? Obviously, we can't just rely on IP Address , because IP The relationship between address and network service is one to many . In fact, by “IP Address + Port number ” To distinguish between different services .

​ IP + port Can uniquely identify a service on a machine on the network .

2、 Internet protocol and OSI Model

(1)osi Seven tier protocol

 application layer : An interface between network services and end users
The presentation layer : Presentation of data 、 Security 、 Compress .( The application layer has been incorporated into the five layer model )
The session layer : establish 、 management 、 Terminate the conversation .( The application layer has been incorporated into the five layer model )
Transport layer : Define the protocol port number of data transmission , And flow control and error checking .
The network layer : Logical address addressing , Realize path selection between different networks .
Data link layer : Establish logical connections 、 Address the hardware 、 Error checking and other functions .( The protocol is defined by the underlying network )
The physical layer : establish 、 maintain 、 Disconnect the physical connection .( The protocol is defined by the underlying network )

(2)osi Five layer agreement

 application layer ( Five floors )
Transport layer ( four layers ) port UDP agreement / TCP agreement Four layer switch Four layer router
The network layer ( Three layers ) IPV4 agreement / IPV6 agreement Router Three layer switch
Data link layer ( On the second floor ) mac arp agreement network card ( On the second floor ) Switch
The physical layer ( First floor )

3、TCP/UDP agreement

(1)、TCP agreement

​ When the application wants to pass TCP When communicating with another application , It will send a communication request . The request must be sent to an exact address . On both sides “ handshake ” after ,TCP A full duplex will be established between the two applications (full-duplex) Communication for . This full duplex communication will take up the communication line between two computers , Until it is closed by one or both parties .

purpose : Upload , download
characteristic : Safe and reliable 、 Connection oriented 、 Slow speed 、 The length of data that can be transmitted is unlimited
The process :
 Establishing a connection -- Three handshakes
The messaging -- Reliable transmission
disconnect -- Four waves
TCP The process of three handshakes is as follows :
 The client sends SYN(SEQ=x) The message is sent to the server , Get into SYN_SEND state .
Server side received SYN message , In response to a SYN (SEQ=y)ACK(ACK=x+1) message , Get into SYN_RECV state .
The client receives... From the server SYN message , In response to a ACK(ACK=y+1) message , Get into Established state .
Three handshakes complete ,TCP The client and server successfully established the connection , It's time to start transmitting data .
TCP The process of four waves is as follows :
 Three handshakes are required to establish a connection , And it takes four waves to terminate a connection , This is from TCP Half closed (half-close) Caused by the .
(1) An application process first calls close, Call this end to execute “ Active shut down ”(active close). The end of TCP So send a FIN Section by section , Indicates that the data transmission is completed .
(2) Received this FIN The end-to-end execution of “ Passive closure ”(passive close), This FIN from TCP confirm .
Be careful :FIN Receive also as a file Terminator (end-of-file) Pass it to the receiving application process , After any other data that has been queued for the application process to receive , because ,FIN Receiving means that the receiving application process has no additional data to receive on the corresponding connection .
(3) After a while , The application process receiving the file terminator will call close Close its socket . This leads to its TCP Also send a FIN.
(4) Take over this final FIN The original sender of TCP( That is to say, the active closing end ) Confirm this FIN.
Since you need one in every direction FIN And a ACK, So it's usually necessary to 4 Segments .
Be careful :
(1) “ Usually ” Refer to , In some cases , step 1 Of FIN Send with the data , in addition , step 2 And steps 3 The segments sent are from the end of the execution of the passive shutdown , It may be merged into a section .
(2) Steps in 2 And steps 3 Between , It is possible to flow data from the passive closing end to the active closing end , This is called “ Half closed ”(half-close).
(3) When one Unix The process, whether voluntarily ( call exit Or from main The function returns ) Or involuntarily ( Received a signal to terminate the process ) At the end of , All open descriptors are closed , This also leads to anything that is still open TCP The link also sends out a FIN.
Whether it's the client or the server , Active shutdown can be performed at either end . Usually it's , The customer performs an active shutdown , But some agreements , for example ,HTTP/1.0 The server does the active shutdown .

img

(2)、UDP agreement

​ When the application wants to pass UDP When communicating with an application , No connection between source and terminal is established before data transmission . When it wants to transfer, it simply grabs the data from the application , And throw it on the Internet as soon as possible .

purpose : Instant messaging tools
characteristic : unreliable 、 For datagram 、 Fast 、 The length of data that can be transmitted is limited
The process :

Whether the other person is online or not Direct transmission

No connection

You can send and receive messages at any time

(3)、TCP and UDP Comparison :

​ TCP --- Transmission control protocol , It provides connection oriented 、 Reliable byte stream service . Before the client and server exchange data with each other , We must first establish a TCP Connect , Then we can transfer the data .TCP Provide overtime resend , Discard duplicate data , Test data , Flow control and other functions , Make sure the data can be transferred from one end to the other .
​ UDP --- User datagram protocol , Is a simple datagram oriented transport layer protocol .UDP No reliability , It just passes the application to IP The datagram of the layer is sent out , But there is no guarantee that they will reach their destination . because UDP There is no need to establish a connection between the client and the server before transmitting the datagram , And there is no mechanism such as overtime retransmission , So the transmission speed is very fast .

 Now? Internet The popular protocol is TCP/IP agreement , In the agreement, for the amount below 1024 There are exact definitions for the ports of , They correspond Internet Some common services . These common services can be divided into the use of TCP port ( Connection oriented ) And use UDP port ( Facing connectionless ) Two kinds of .
Speaking of TCP and UDP, First of all, understand “ Connect ” and “ There is no connection ” The meaning of , Their relationship can be illustrated by a figurative metaphor , It's phone calls and letters . If two people want to talk , The first thing to do is to establish a connection —— It's the dial when you call , Wait for a response —— After answering the phone , In order to communicate information to each other , And finally disconnect —— Just hang up . It's easier to write a letter , Fill in the recipient's address and drop the letter into the mailbox , The recipient will receive . From this analysis we can see that , Establishing a connection can create a channel between the two parties that need to grieve , After the sender sends the request connection information and the receiver responds , Because it's after the receiver responds that the message begins to pass , And it's in one channel , Therefore, the receiver can receive more complete information from the sender , That is, the reliability of information transmission is relatively high . But it also needs to be connected , Increase the cost of resources ( You must wait for the receiver to respond before establishing a connection , In the process of transmitting information, it is necessary to confirm whether the information is transmitted or not and send out the corresponding signal when the connection is disconnected ), Monopolize a channel , Money can't make another connection after disconnection , In other words, a third party can't make a call during a conversation . And no connection is sending information in the first place ( Strictly speaking, , This is no beginning 、 The end of the ), It's just a one-time pass , No response from the receiver is required , Therefore, to a certain extent, it can not guarantee the reliability of information transmission , It's like writing a letter , We're just sending letters , There is no guarantee that the recipient will receive .
TCP It's connection-oriented , There is a high reliability , Some of the more demanding services generally use this protocol , Such as FTP、Telnet、SMTP、HTTP、POP3 etc. .
UDP It's for connectionless , Common services that use this protocol are DNS、SNMP、QQ etc. . about QQ It must be stated in addition that ,QQ2003 It used to only use UDP Agreed , Its server uses 8000 port , Listen for information coming from , Client side usage 4000 port , Send information out ( This is not difficult to understand in general IP Of QQ Show friends in the version IP The port in the address information is usually 4000 Or the reason for its subsequent ports ), namely QQ Programs both accept and provide services , In the future QQ Version also supports the use of TCP It's agreed .
版权声明
本文为[Yongliang]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database