Read the rest of the transaction. :param sock: The socket to drain. :param num_bytes_to_drain: The number of bytes to drain. If -1, then drain bytes until the final zero-length chunk is read.
(sock: socket.socket, previously_read_data: bytes, num_bytes_to_drain: int)
| 67 | |
| 68 | |
| 69 | def drain_socket(sock: socket.socket, previously_read_data: bytes, num_bytes_to_drain: int) -> None: |
| 70 | """Read the rest of the transaction. |
| 71 | |
| 72 | :param sock: The socket to drain. |
| 73 | :param num_bytes_to_drain: The number of bytes to drain. If -1, then drain |
| 74 | bytes until the final zero-length chunk is read. |
| 75 | """ |
| 76 | |
| 77 | read_data = previously_read_data |
| 78 | num_bytes_drained = 0 |
| 79 | while True: |
| 80 | if num_bytes_to_drain > 0: |
| 81 | if num_bytes_drained >= num_bytes_to_drain: |
| 82 | break |
| 83 | elif b'0\r\n\r\n' == read_data[-5:]: |
| 84 | print("Found end of chunked data.") |
| 85 | break |
| 86 | |
| 87 | data = sock.recv(1024) |
| 88 | print(f'Received:\n{data}') |
| 89 | if not data: |
| 90 | print("Socket closed.") |
| 91 | break |
| 92 | num_bytes_drained += len(data) |
| 93 | read_data += data |
no test coverage detected