Quero ler todos os bytes disponíveis no buffer, o código abaixo lido por blocos de 100 bytes.
1) É possível ler todos os bytes que estão no buffer?
2) qual é o valor padrão de sock.recv ()? // sem argumento
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) E se eu definir sock.recv (1000000), ele lerá todo o buffer, certo? (suponha que a velocidade dos dados seja muito menor)
O objetivo principal é ler continuamente um fluxo
1) É possível ler todos os bytes que estão no 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?
Ele só lerá até o número determinado de bytes. A quantidade de dados que ele irá ler na realidade também dependerá do tamanho do buffer de soquete subjacente no kernel, ou seja, ele não pode ler nenhum dado que ainda não esteja no buffer de soquete, mesmo que o cliente já os tenha enviado.
Outra restrição se aplica se o soquete for um soquete SSL. Nesse caso recv
, geralmente retornará apenas os dados do quadro SSL atual.
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras