python network programming calls the recv function three ways to receive data in its entirety
- 2020-05-27 06:27:50
- OfStack
Recently developed a common tcpclient testing tool for network programming using python. In the network programming with socket, how to determine whether a message sent to the end has been received or not is a problem that must be considered in the network development of socket. Here, a brief introduction is given to 3 methods commonly used to judge the completion of data reception and reception:
1. Basic data receiving method:
When using the underlying data receive method, an empty string is received when disconnected from the service socket. Therefore, according to this feature, a loop can be added to the program to receive data directly until the data sender closes the socket connection. Scenario: the link between the client and the server is short (that is, the connection will be closed after one socket communication). The code is as follows:
import socket,struct,sys,time
Port=22220
#assume a socket disconnect (data returned is empty string) means all data was #done being sent.
def recv_basic(the_socket):
total_data=[]
while True:
data = the_socket.recv(20480)
if not data: break
total_data.append(data)
return ''.join(total_data)
2. Tail identification method
The datagram of the complete data is obtained by looking for the tail identification string in the received protocol data. Applicable scenario: the received protocol data contains the associated tail identity. The code is as follows:
End='something useable as an end marker'
def recv_end(the_socket):
total_data=[];data=''
while True:
data=the_socket.recv(8192)
if End in data:
total_data.append(data[:data.find(End)])
break
total_data.append(data)
if len(total_data)>1:
#check if end_of_data was split
last_pair=total_data[-2]+total_data[-1]
if End in last_pair:
total_data[-2]=last_pair[:last_pair.find(End)]
total_data.pop()
break
return ''.join(total_data)
3. Load length method
That is, the length of valid message can be determined by the load length value in the protocol data. Applicable scenario: the protocol data contains the load protocol field. This method is also a common and general method, but requires one side to receive data and one side to parse data. The code is as follows:
def recv_size(the_socket):
#data length is packed into 4 bytes
total_len=0;total_data=[];size=sys.maxint
size_data=sock_data='';recv_size=8192
while total_len<size:
sock_data=the_socket.recv(recv_size)
if not total_data:
if len(sock_data)>4:
size_data+=sock_data
size=struct.unpack('>i', size_data[:4])[0]
recv_size=size
if recv_size>524288:recv_size=524288
total_data.append(size_data[4:])
else:
size_data+=sock_data
else:
total_data.append(sock_data)
total_len=sum([len(i) for i in total_data ])
return ''.join(total_data)