BlackHATPython study notes,

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.