diff options
author | Ben Hutchings <ben.hutchings@codethink.co.uk> | 2020-04-30 23:46:42 +0100 |
---|---|---|
committer | Ben Hutchings <ben.hutchings@codethink.co.uk> | 2020-05-06 13:30:03 +0100 |
commit | 1dd8feaee55c5f530528b59019a2ad3c357f2905 (patch) | |
tree | ad9ad67cecf1c305bc084c8e73052f77acfbf8e8 /lorry-controller-minion | |
parent | 78a5cd5a045dd94bdcf8ed5b09226f8bf2daca39 (diff) | |
download | lorry-controller-1dd8feaee55c5f530528b59019a2ad3c357f2905.tar.gz |
MINION: Convert binary data to text as necessary
* HTTP responses are bytes but json.load wants a str. Decode as UTF-8
in strict mode, on the assumption that WEBAPP will always produce
valid UTF-8.
* os.read returns bytes. Decode as UTF-8 in non-strict (replace)
mode.
Diffstat (limited to 'lorry-controller-minion')
-rwxr-xr-x | lorry-controller-minion | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/lorry-controller-minion b/lorry-controller-minion index 6e2affd..0412629 100755 --- a/lorry-controller-minion +++ b/lorry-controller-minion @@ -126,7 +126,7 @@ class MINION(cliapp.Application): logging.error(str(e)) return None - obj = json.loads(body) + obj = json.loads(body.decode('utf-8')) if obj.get('job_id', None): return obj return None @@ -203,7 +203,8 @@ class MINION(cliapp.Application): r, w, x = select.select([self.stdout_fd], [], [], wait_for_output) stdout = stderr = '' if r: - stdout = os.read(self.stdout_fd, read_size) + stdout = os.read(self.stdout_fd, read_size) \ + .decode('utf-8', errors='replace') else: # Finished. if exit != 0: @@ -213,7 +214,7 @@ class MINION(cliapp.Application): data = os.read(self.stdout_fd, read_size) if not data: break - stdout_parts.append(data) + stdout_parts.append(data.decode('utf-8', errors='replace')) stdout = ''.join(stdout_parts) stderr = '' os.remove(self.temp_lorry_filename) @@ -251,7 +252,7 @@ class MINION(cliapp.Application): logging.error(str(e)) return - obj = json.loads(body) + obj = json.loads(body.decode('utf-8')) return obj['kill'] def webapp_request(self, method, path, body): @@ -275,7 +276,8 @@ class MINION(cliapp.Application): conn.close() if response.status != http.client.OK: - raise WEBAPPError(response.status, response.reason, response_body) + raise WEBAPPError(response.status, response.reason, + response_body.decode('utf-8', errors='replace')) return response_body |