| 1294 | |
| 1295 | |
| 1296 | def read_data(connection, cmd, pos_nl, max_data_size, check_done, cmd_name, timeout=10): |
| 1297 | data = cmd[pos_nl+1:] |
| 1298 | t = 0.0 |
| 1299 | try: |
| 1300 | while (len(data) < max_data_size) and (not check_done or not data.endswith('\nDONE')) and (timeout > 0 and t < timeout): |
| 1301 | bytes_received = connection.recv(1024) |
| 1302 | if bytes_received: |
| 1303 | try: |
| 1304 | text_received = bytes_received.decode('ascii', 'ignore') |
| 1305 | except UnicodeDecodeError as e: |
| 1306 | print_ts('Error: Decoding failed ({}): {}'.format(cmd_name, e)) |
| 1307 | data = None |
| 1308 | break |
| 1309 | t = 0.0 |
| 1310 | data += text_received |
| 1311 | elif not check_done: |
| 1312 | break |
| 1313 | else: |
| 1314 | time.sleep(0.2) |
| 1315 | t += 0.2 |
| 1316 | except socket.error as e: |
| 1317 | print_ts('Socket error occurred ({}): {}'.format(cmd_name, e)) |
| 1318 | data = None |
| 1319 | |
| 1320 | connection.close() |
| 1321 | |
| 1322 | if (timeout > 0) and (t >= timeout): |
| 1323 | print_ts('Timeout occurred ({}).'.format(cmd_name)) |
| 1324 | data = None |
| 1325 | |
| 1326 | if data and (len(data) >= max_data_size): |
| 1327 | print_ts('Maximum allowed data ({} bytes) exceeded ({}).'.format(max_data_size, cmd_name)) |
| 1328 | |
| 1329 | elif data and check_done and not data.endswith('\nDONE'): |
| 1330 | print_ts('Incomplete data received ({}).'.format(cmd_name)) |
| 1331 | data = None |
| 1332 | |
| 1333 | return data |
| 1334 | |
| 1335 | def server(server_address_port: int, packages: list, packageIndex: int, resultPath: str) -> None: |
| 1336 | socket.setdefaulttimeout(30) |