我有一个TCP处理数据流。它一次需要4096个字节,但我需要一种方法来在它恢复之前找出整个套接字大小。这样我就可以确定在切换套接字之前读取套接字的次数。它还会通知这是这个套接字的最后一个recv,这样我就可以在选择切换到另一个套接字之前用它做一些事情。
def handle_tcp(self, sock, remote):
fdset = [sock, remote]
while True:
r, w, e = select.select(fdset, [], [])
# How to find out how much bytes need to be read from socket?
if sock in r:
non_blocking_calculation()
if remote.send(sock.recv(4096)) <= 0: break
if remote in r:
non_blocking_calculation()
if sock.send(remote.recv(4096)) <= 0: break
答案 0 :(得分:2)
select()
之后。
import fcntl, termios, array, math
...
sock_size = array.array('i', [0]) # initialise outside While loop
if sock in r:
fcntl.ioctl(sock, termios.FIONREAD, sock_size)
count = ceil(sock_size[0] / float(BUF_SIZE))
print("Socket size: {} and count: {}".format(sock_size[0], count)
答案 1 :(得分:1)