darkpool
darkpool

Reputation: 14641

Sockets - buffering data

I am pulling data from a 3rd party api and im just looking to understand socket.recv(bufsize)

Here's a snippet:

def readlines(sock, recv_buffer=4096, delim='\n'):
    buffer = ''
    data = True
    while data:
        data = sock.recv(recv_buffer)
        buffer += str(data.decode('latin-1'))

        while buffer.find(delim) != -1:
            line, buffer = buffer.split('\n', 1)
            yield line
    return

def main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    sock.sendall(request_message.encode())
    for line in readlines(sock):
        # do some stuff

Everything works, im just trying to understand exactly what is happening in terms of the buffer. Which one of the following is happening?

I presume it's the first option, but I just wanted to check and understand this better.

Also, does a buffer have to be used when requesting data?

Upvotes: 0

Views: 158

Answers (1)

Barmar
Barmar

Reputation: 780688

TCP and UDP don't have any way to "request" data. This can be done in the application protocol, but the socket functions don't know anything about this. They just deal with the raw data.

So the server sends the data at whatever times it wants to. The buffer size you specify is the maximum that recv will will process at once, but it could return less than this. In the case of datagram (UDP) sockets, it returns the contents of one datagram per call. In the case of stream (TCP) sockets, it returns whatever happens to have been received so far, up to the limit.

Upvotes: 1

Related Questions