BlackHATPython study notes,
Chapter 2 replacing netcat
At first, I was not very familiar with the following code:
1 def client_sender (buffer): 2... 3 4 while True: 5 len_recv = 1 6 response = "" 7 8 while len_recv: 9 data = client. recv (4096) 10 len_recv = len (data) 11 response + = data12 13 if len_recv <4096: # I don't quite understand why this is done here 14 break15 16 print (response) 17...
Then, I found detailed descriptions of the socket. recv function on the Internet:
Recv checks the receiving buffer of socket s. If there is no data in the receiving buffer or the Protocol is receiving data, the recv waits until the Protocol receives the data. When the Protocol receives the data, the recv function copies the data in the s receiving buffer to the buf (note that the data received by the protocol may be larger than the buf length, so in this caseYou need to call the recv function several times to copy the data in the s receiving buffer.End. The recv function only copies data, and the real data reception is completed by the Protocol). The recv function returns the actual number of bytes of the copy.
Therefore, the above judgment is to determine whether the data is accepted. If the data is received, the loop is exited and the received message is printed.