Nik Konst
Nik Konst

Reputation: 67

How to read all available bytes in a socket buffer? Python3

I want to read all available bytes in the buffer, the code below read by chunks of 100 bytes..

1) Is it possible to read all bytes are in the buffer?

2) what is the default value of sock.recv()? // no argument

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('localhost', 1489)
    sock.connect(server_address)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 8192)  # Buffer size 8192
    sock.setblocking(0) # set non-blocking

    while 1:
        read_ready = select.select([sock], [], [], timeout_in_seconds)  
        if read_ready[0]: # we have what to read
            print('ready')
            data = sock.recv(100)
            if not data:
                break
            print(data)

UPD: 3) What if I set sock.recv(1000000), will it read all the buffer, right? (assume that data speed is much less)

Main purpose is continuously reading a stream

Upvotes: 1

Views: 3561

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123320

1) Is it possible to read all bytes are in the buffer?

recv has no idea how many bytes are in the socket buffer or how many might arrive while trying to read from the buffer. But you can either use recv on a non-blocking socket and read until you get an error because no more data are available (errno.EWOULDBLOCK). Or you could use a zero timeout (0, not None) in select.select to just check (poll) if there are still data to read.

2) what is the default value of sock.recv()? // no argument

The documentation for recv says clearly how this function needs to be called:

socket.recv(bufsize[, flags])

Thus, bufsize is not in argument you can skip and your attempt to do this should result in an error.

3) What if I set sock.recv(1000000), will it read all the buffer, right?

It will only read up to the given number of bytes. How many it will read in reality will also depend on the size of the underlying in-kernel socket buffer, i.e. it can not read any data which are not yet in the socket buffer even if the client has already sent them.

Another restriction applies if the the socket is an SSL socket. In this case recv will usually only return the data from with the current SSL frame.

Upvotes: 1

Related Questions