Python -- network programming practice

Hee hee hee hee hee 2020-11-13 01:57:51
python network programming practice

One . Common network programming interview questions

1. sketch OSI Seven tier protocol .

OSI Is an open communication system interconnection reference model .OSI The model has 7 The layer structure , Each layer can have several sub layers . OSI Of 7 The layers from top to bottom are 7 application layer 6 The presentation layer 5 The session layer 4 Transport layer 3 The network layer 2 Data link layer 1 The physical layer ; Among them, the top ( namely 7、6、5、4 layer ) Defines the capabilities of the application , below 3 layer ( namely 3、2、1 layer ) Mainly for end-to-end data flow through the network .

2. sketch TCP/IP Four layer protocol .

application layer :HTTP
Transport layer :TCP
The network layer :IP
The link layer : The Internet

application layer : The type of communication this time determines , for instance FTP、DNS、SMTP wait , meanwhile HTTP Protocols also fall into the scope of the application layer . Popular speaking , The application layer decides what to do with this communication .

Transport layer : The transport layer provides data transfer between two computers , There are two very popular protocols in the transport layer , Namely TCP and UDP agreement . Three handshakes often mentioned in an interview , Four waves are TCP Part of the agreement

The network layer : The network layer is used to process these flowing packets , That is, if the corresponding packets are routed to the specified location , Select transmission route for network transmission during communication

Data link layer : The data link layer contains the interface between software and hardware , And the hardware of various network devices , That is, the infrastructure at the bottom of the whole network communication process

3. TCP and UDP What's the difference ?

 Insert picture description here

4. TCP When the connection is established 3 The specific process of handshake , And why every step of it is ?

Three handshakes
TCP The connection is made by three handshakes .

The first handshake

When a client initiates a connection request to the server , The client will send the synchronization sequence label SYN To the server , Here we set up SYN by x, Wait for server to confirm , At this time, the status of the client is SYN_SENT.

The second handshake

When the server receives the client sent SYN after , What the server needs to do is to confirm that the client sent it SYN, Here the server sends a confirmation packet ACK, there ACK by x+1, mean “ I received your message SYN 了 ”, meanwhile , The server will also send a SYN package , Here we set SYN by y. At this time, the status of the server is SYN_RECV.
In a word , Server side send SYN and ACK Two bags .

The third handshake

The client receives... From the server SYN and ACK After package , A confirmation packet needs to be sent to the server ACK,“ I also received your message SYN 了 , I'll send you a confirmation , And then we can fit together ”, there ACK by y+1, After sending , The client and server status is ESTABLISH, namely TCP Successful connection .

In three handshakes , Both the client and server send two packets SYN and ACK, But the two packages on the server side are sent at one time , The two packets of the client are sent in two times .

5. TCP The specific process of disconnection , Each of these steps is why you do that ?

Disconnect requests can be made by the client , It can also be sent by the server , Here we call A To the end B End request to disconnect .

First wave

A To the end B When a client requests to disconnect, it will ask B The client sends a message with FIN Marked segment , there FIN yes Finish It means .

Second wave

B End receipt A Sent FIN after ,B Now, maybe there is still data to be transmitted , therefore B The end doesn't go to A End send FIN, Instead, send a confirmation number first ACK, mean “ I received your request for disconnection , But I still have data to send out , Just a moment, please ”.

Third wave

When B The end of the business , So at this time B The end can be disconnected , here B To the end A End send FIN Serial number , It means you can disconnect this time .

Fourth wave

A End receipt B Sent by FIN after , Will send to B End send confirmation ACK, And then through two MSL Disconnect after a long time .

MSL yes Maximum Segment Lifetime, Maximum segment lifetime ,2 individual MSL It is the longest time that a message segment is sent and received

6. TCP Eleven states of ?

state :
service / client :CLOSED( In the beginning , Both the server and the client are down )
Customer :SYN_SENT( Send a connection request ),
service :LISTEN( monitor , Waiting for the connection ),
service : SYN_RECV( Upon receipt of the request , reply ),
service / Customer : ESTABLISHED( Establishing a connection ),
Customer : FIN_WAIT_1( Disconnect waiting 1),
service :CLOSE_WAIT( Turn off waiting , Transfer the data that has not been transmitted ),
client : FIN_WAIT_2( received ACK, Wait for the data transfer to complete ),
service : LAST——ACK( Send a shutdown request ),
Customer : TIME_WAIT( Reply to disconnect request ),
Customer : CLOSING( Something happened to the client FIN, But I didn't receive the server's ACK, But I received the server's FIN. This happens when the server sends a packet loss , Because sometimes there are accidents in network transmission )

7. What is? socket? The brief is based on tcp Protocol socket communication process .

socket( Socket for short ) It's interprocess communication ⼀ Kind of ⽅ type , It can realize inter process communication between different hosts , We ⽹ All kinds of services are available ⼤ Most of them are based on Socket To complete the communication .
Communication process :

  1. Server first socket Function to create a socket , Use this socket to monitor the communication .
  2. use bind Function to bind a port number and IP Address . Because the local computer may have multiple URLs and IP, every last IP And ports have multiple ports . One needs to be specified IP Monitor with port .
  3. Server calls listen function , Make this port of the server and IP In a listening state , Wait for the client to connect .
  4. For the client socket Function to create a socket , Set remote IP And port .
  5. The client calls connect Function to connect to the port specified by the remote computer .
  6. The service is used accept Function to accept connections from remote computers , Establish communication with clients .
  7. After establishing the connection , For the client write Functional direction socket Middle write data . It can also be used. read Function to read the data sent by the server .
  8. The service is used read Function to read the data sent by the client , It can also be used. write Function to send data .
  9. After completing the communication , use close Function off socket Connect .
8. The brief is based on udp Protocol socket communication process .

Communication process :
udp There is no need to establish a connection between the client and the server , So there is no process of establishing a connection in the program . Before you communicate , Need to set up a network socket .
The server needs to bind a port , Listen for received messages on this port .
The client needs to set up remote IP And port , The information that needs to be delivered needs to be sent to this IP And ports .

9. TCP Why not two connections ? It's three handshakes ?

For reliable data transmission , TCP The parties to the agreement , You have to maintain a serial number , To identify the packets sent , What has been received by the other party .
The process of three handshakes is that the communication parties inform each other of the starting value of the serial number , And confirm that the other party has received the start value of the serial number .
If it's just two handshakes , At most, only the starting serial number of the connection initiator can be confirmed , The serial number chosen by the other party is not confirmed .

10. Why based on tcp Protocol communication is based on udp Protocol communication is more reliable ?

TCP A reliable guarantee of , It's a three way handshake mechanism , This mechanism ensures that the data , Guaranteed his reliability . and UDP There is no the ,udp When the message is sent , Don't verify whether it reaches the other party , So it's not reliable .

11. Design concurrent server in network programming , Using multiprocessing and multithreading , What's the difference, please ?

The fundamental difference is : With multiple processes, each process has its own address space (address space), Threads share address space . All the other differences come from this :

  1. Speed : Fast thread generation , The communication between threads is fast 、 Switch quickly and so on , Because they're in the same address space .
  2. Resource utilization : The resource utilization of threads is better because they are in the same address space .
  3. Synchronization problem : Threads use common variables / Do I need to use synchronization mechanism when I use memory or because they are in the same address space .

Advantages of multithreading :

  1. The cost of creating and environment switching is low
  2. Synchronization costs less ( Synchronization between processes usually involves OS kernel )
  3. Data replication can be done by “ Process local memory ”

The limitations of multithreading :

  1. Performance loss
    (1) Single processor 、“ Computing is heavy ” Applications that don't benefit from multithreading
    (2) High precision locking strategy will bring high synchronization cost
  2. Less robust
    Received between threads “MMU Protect ” Little or no
  3. Lack of high precision access control
12. Connect on a large scale , How to design concurrency model ?

Large scale connectivity , First, exclude multiprocessing implementations , Large scale connections using multiple processes require a lot of memory space
Consider using a multithreaded server or this coprocessor to implement a concurrent server .

  • Coprocessor to achieve server advantages :
    1. No thread context switching overhead
    2. There is no need for atomic locking and synchronization
    3. Easy to switch control flow , Simplify the programming model
    4. High concurrency + High scalability + Low cost : One CPU Supporting tens of thousands of projects is not a problem . So it is very suitable for high concurrency processing .

  • shortcoming :
    1. Can't use multi-core resources : The essence of a process is a single thread , It cannot at the same time Single CPU For multiple cores , The process needs to work with the process to run in multiple CPU On . Of course, most of the applications we write in our daily life don't have to , Unless it is cpu Intensive application .
    2. To block (Blocking) operation ( Such as IO when ) It will block the whole program

Two . Common network programming written test questions .

  1. A
     Insert picture description here
    analysis :TCP/IP The transport layer of the agreement is TCP and UDP Two agreements .

  2. D Insert picture description here
    analysis : Convert a domain name into something that the network can recognize ip Address , And then the actual network communication .

  3. B
     Insert picture description here
    analysis : The connectionless system is UDP Service delivery , Just throw out the byte stream

  4. C
     Insert picture description here
    analysis :IP The address is expressed in dotted decimal system

  5. D
     Insert picture description here
    analysis :OSI The data units exchanged between layers are called service data units SDU.

  6. B
     Insert picture description here
    analysis :TCP It's a unicast two-way service

  7. A
     Insert picture description here
    analysis :TCP The protocol uses three handshakes to connect

  8. C
     Insert picture description here
    analysis : The so-called port , It's like a house number , The client can use the ip Address to find the corresponding server , But there are many ports on the server side , Each application corresponds to a port number .

  9. E
     Insert picture description here analysis : Fight for the TCP The handshake process is :SYN,SYN+ACK,ACK

  10. C
     Insert picture description here
    analysis :OSI The application layer is from the top to the bottom of the seven layers , The presentation layer , The session layer , Transport layer , The network layer , Data link layer , The physical layer .

  11. C
     Insert picture description here
    analysis :ACK It should be transmitted from SYN+1 namely ACK=x+1,SYN unchanged ,Seq by y

  12. D
     Insert picture description here
    analysis : The network is classified as follows
     Insert picture description here

  13. D
     Insert picture description here
    analysis :RARP agreement : The physical address can be converted to IP Address
    ARP agreement : Can be IP Address translation to physical address

  14. C
     Insert picture description here
    analysis :Netstat: Shows which network connections are working . Default display TCP Connected information .
    ping: Check network connectivity , It can help us analyze and judge the network fault
    Ipconfig: Show IP Address 、 Subnet mask and default gateway values .
    Nslookup: Inquire about DNS The record of , Check whether domain name resolution is normal , It is used to diagnose network problems when the network fails .

  15. C
     Insert picture description here
    analysis : The main elements are generally understood : How to say it , What to say , When to talk

本文为[Hee hee hee hee hee]所创,转载请带上原文链接,感谢

  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