Python Socket API Reference Source: http://blog.csdn.net/xiangpingli/article/details/47706707
Use SOCKET.RECV (pack_length) to receive an indeterminate amount of data, if the packet length exceeds a certain value, then the data received is not complete.
Refer to the python3.4 documentation to discover:
- socket. recv (
bufsize[,
flags])
-
Receive data from the socket. The return value is a bytes object representing the data received. The maximum amount of data to being received at once are specified by bufsize.
The general meaning of the above English is: receive data from the socket. The return value is the Byts type. The maximum number of bytes received is the buffer size.
[Email protected]:~# cat setsockopt_test.py #!/usr/bin/pythonimport socketsend_buf_size = 4096 # send buffer size recv_buf_ size = 4096 # Receive buffer sizes Def modify_buff_size (): sock = Socket.socket (socket.af_inet, socket. SOCK_STREAM) bufsize = sock.getsockopt (socket. Sol_socket, SOCKET. SO_SNDBUF) print "Buffer size [before]:%d"%bufsize sock.setsockopt (socket). SOL_TCP, Socket. Tcp_nodelay, 1) sock.setsockopt (socket. Sol_socket, SOCKET. SO_SNDBUF, Send_buf_size) sock.setsockopt (socket. Sol_socket, SOCKET. So_rcvbuf, recv_buf_size) bufsize = sock.getsockopt (socket. Sol_socket, SOCKET. SO_SNDBUF) print ("Buffer size [after]:%d"%bufsize) if __name__ = = ' __main__ ': modify_buff_size ()
Perform
BufSize = sock.getsockopt (socket. Sol_socket, SOCKET. SO_SNDBUF)
Get the default receive buffer value on the computer: 8192
Problem Scenario Description:
Server-side non-Python implementation, the client side is Python3 implementation, the communication protocol is a custom format, each interaction of the packet is an indefinite length of packets.
Fetch data from server side, because no query condition is specified, the data of the returned data is about 1000, and each piece of data contains multiple integer and string type, byte size is: 26782.
If the amount of data obtained is more, each request data, the client's SOCKET.RECV (packet_size) will be executed multiple times.
Solution:
Receiverbufsize = self._client_socket.getsockopt ( socket. Sol_socket, SOCKET. SO_RCVBUF) data_body = None if receiverbufsize < pack_length: data_body = bytes () left_pack_length = Pack_length while left_pack_length > 0: if left_pack_length > receiverbufsize: body_part =self._ CLIENT_SOCKET.RECV (receiverbufsize) else: body_part =self._client_socket.recv (left_pack_length) Data_body + = Body_part left_pack_length-= receiverbufsize else: data_body= self._client _SOCKET.RECV (Pack_length)
Personal notes:
The Python socket can read only the entire data of the buffer at a time, and if the specified packet size is larger than the buffer size, the valid data read is only the buffer's data.
If you can determine that the data sent is larger than the size of the buffer, it needs to be multiple times: Socket.recv (receiverbufsize), then the received data is stitched into a complete packet before parsing.
The Python3 TCP socket receives an indeterminate amount of data that is received by the packet.