Skip to content

Commit

Permalink
Merge pull request #156 from iammattcoleman/replace-file-with-stringio
Browse files Browse the repository at this point in the history
Use StringIO as a buffer instead of a file
  • Loading branch information
maurizio-lombardi committed Jan 27, 2020
2 parents 94d971d + 3db7531 commit 23877ab
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions daemon/targetclid
Expand Up @@ -32,6 +32,7 @@ import struct
import fcntl
import signal
import errno
import io


err = sys.stderr
Expand Down Expand Up @@ -153,7 +154,7 @@ class TargetCLI:
connection.close()
still_listen = False
else:
self.con._stdout = self.con._stderr = f = open("/tmp/data.txt", "w")
self.con._stdout = self.con._stderr = f = io.StringIO()
try:
# extract multiple commands delimited with '%'
list_data = data.decode().split('%')
Expand All @@ -165,14 +166,14 @@ class TargetCLI:
# Restore
self.con._stdout = self.con_stdout_
self.con._stderr = self.con_stderr_
f.close()

with open('/tmp/data.txt', '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
output = f.getvalue()
var = struct.pack('i', len(output))
connection.sendall(var) # length of string
if len(output):
connection.sendall(output.encode()) # actual string

f.close()


def usage():
Expand Down

0 comments on commit 23877ab

Please sign in to comment.