diff options
author | Maurizio Lombardi <mlombard@redhat.com> | 2020-04-06 09:41:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-06 09:41:34 +0200 |
commit | e09fa8c531b4a4d2972df8e96e0baf0bdd953715 (patch) | |
tree | b93a3961b8323239967029c2dd60112179d69dcb | |
parent | 18ce78b530f01807b2f061e821b9329357bc5e02 (diff) | |
parent | 4f6611ed208b6029b4523ca7760a802e82b6a199 (diff) | |
download | targetcli-e09fa8c531b4a4d2972df8e96e0baf0bdd953715.tar.gz |
Merge pull request #168 from pkalever/temp-files
Use temp file objects for temporary storage area
-rwxr-xr-x | daemon/targetclid | 26 | ||||
-rwxr-xr-x | scripts/targetcli | 3 |
2 files changed, 15 insertions, 14 deletions
diff --git a/daemon/targetclid b/daemon/targetclid index 5df6ca2..329cede 100755 --- a/daemon/targetclid +++ b/daemon/targetclid @@ -26,19 +26,16 @@ from configshell_fb import ConfigShell from os import getuid, getenv, unlink from threading import Thread +import os import sys import socket import struct import fcntl import signal import errno +import tempfile -if sys.version_info < (3, 0): - from io import BytesIO as StringIO -else: - from io import StringIO - err = sys.stderr class TargetCLI: @@ -158,26 +155,29 @@ class TargetCLI: connection.close() still_listen = False else: - self.con._stdout = self.con._stderr = f = StringIO() + self.con._stdout = self.con._stderr = f = tempfile.NamedTemporaryFile(mode='w', delete=False) try: # extract multiple commands delimited with '%' list_data = data.decode().split('%') for cmd in list_data: self.shell.run_cmdline(cmd) except Exception as e: - print(str(e).encode(), file=f) # push error to stream + print(str(e), file=f) # push error to stream # Restore self.con._stdout = self.con_stdout_ self.con._stderr = self.con_stderr_ - output = f.getvalue().encode() - var = struct.pack('i', len(output)) - connection.sendall(var) # length of string - if len(output): - connection.sendall(output) # actual string - f.close() + with open(f.name, 'r') as f: + output = f.read() + var = struct.pack('i', len(output)) + connection.sendall(var) # length of string + if len(output): + connection.sendall(output.encode()) # actual string + + os.unlink(f.name) + def usage(): print("Usage: %s [--version|--help]" % sys.argv[0], file=err) diff --git a/scripts/targetcli b/scripts/targetcli index 04e5aba..f11ece4 100755 --- a/scripts/targetcli +++ b/scripts/targetcli @@ -154,8 +154,9 @@ def call_daemon(shell, req): # get the actual data in chunks while amount_received < amount_expected: data = sock.recv(1024) + data = data.decode() amount_received += len(data) - print(data.decode(), end ="") + print(data, end ="") sock.send(b'-END@OF@DATA-') sock.close() |