|
@@ -22,33 +22,51 @@ def serve(port, bindTo, path_report_store=None):
|
|
|
s.listen(1)
|
|
|
print('DebugReport server listening on [{0}]:{1}'.format(bindTo, port))
|
|
|
|
|
|
+ try:
|
|
|
+ from cStringIO import StringIO # Python 2
|
|
|
+ except ImportError:
|
|
|
+ from io import StringIO # Python 3
|
|
|
+ report_buffer = StringIO()
|
|
|
+
|
|
|
while 1:
|
|
|
conn, addr = s.accept()
|
|
|
conn.settimeout(30.0)
|
|
|
|
|
|
- report_id = myrandom(10)
|
|
|
+ report_id = None
|
|
|
+ try:
|
|
|
+ while 1:
|
|
|
+ data = conn.recv(BUFFER_SIZE)
|
|
|
+ if not data: break
|
|
|
+ report_buffer.write(data) # python will convert \n to os.linesep
|
|
|
+ report_id = myrandom(10)
|
|
|
+ except:
|
|
|
+ # try to send a zero-length response back to the peer to indicate a problem
|
|
|
+ try:
|
|
|
+ conn.sendall("\r\n")
|
|
|
+ finally:
|
|
|
+ report_buffer.truncate(0)
|
|
|
+ conn.close()
|
|
|
+ continue
|
|
|
+
|
|
|
+ # don't want to receive any more data from the client
|
|
|
+ conn.shutdown(socket.SHUT_RD)
|
|
|
filename = os.path.join(
|
|
|
- path_report_store, "%s_%s.log" % (report_id, datetime.date.today().strftime('%Y-%m-%d_'))
|
|
|
+ path_report_store,
|
|
|
+ "%s_%s.log" % (datetime.date.today().strftime('%Y-%m-%d_'), report_id)
|
|
|
)
|
|
|
-
|
|
|
with open(filename, 'w') as f:
|
|
|
try:
|
|
|
- while 1:
|
|
|
- data = conn.recv(BUFFER_SIZE)
|
|
|
- if not data: break
|
|
|
- f.write(data) # python will convert \n to os.linesep
|
|
|
+ f.write(report_buffer.getvalue())
|
|
|
+ f.flush()
|
|
|
except:
|
|
|
- # delete the incompletely received report
|
|
|
- f.close()
|
|
|
- os.remove(filename)
|
|
|
- # try to send a zero-length response back to the peer to indicate a problem
|
|
|
try:
|
|
|
conn.sendall("\r\n")
|
|
|
finally:
|
|
|
+ report_buffer.truncate(0)
|
|
|
conn.close()
|
|
|
continue
|
|
|
- f.flush()
|
|
|
# f will be closed automatically by leaving the 'with'-scope
|
|
|
+ report_buffer.truncate(0)
|
|
|
|
|
|
# send reply to reportee
|
|
|
response = "%s\r\n" % report_id
|
|
@@ -105,4 +123,4 @@ if __name__ == '__main__':
|
|
|
with daemonContext:
|
|
|
serve(port, bindTo, path_report_store=path_report_store)
|
|
|
else:
|
|
|
- serve(port, bindTo, path_report_store=path_report_store)
|
|
|
+ serve(port, bindTo, path_report_store=path_report_store)
|