using bytearray with socket.recv_into

前端 未结 1 1349
無奈伤痛
無奈伤痛 2021-02-05 14:34

I am doing some socket IO, and using a bytearray object as a buffer. I would like to receive data with an offset into this buffer using csock.recv_into as shown below in order

1条回答
  •  梦毁少年i
    2021-02-05 14:59

    Use a memoryview to wrap your bytearray:

    buf = bytearray(toread)
    view = memoryview(buf)
    while toread:
        nbytes = sock.recv_into(view, toread)
        view = view[nbytes:] # slicing views is cheap
        toread -= nbytes
    

    0 讨论(0)
提交回复
热议问题