Browse Source

Merge branch 'master' into 'master'

ffpb-debug nixio-migration related convenience and stability patches

Just a few stability and convenience additions that came to my eyes while updating the ffpb-debug package to work with the nixio package (instead of os.execute("... | nc ...")).
Michael Schwarz 9 years ago
parent
commit
32702f242d
1 changed files with 24 additions and 9 deletions
  1. 24 9
      server.py

+ 24 - 9
server.py

@@ -14,6 +14,7 @@ def server(port, bindTo):
 	BUFFER_SIZE = 1024
  
 	s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
+	s.settimeout(30.0)
 	s.bind((bindTo, port))
 	s.listen(1)
 	print('DebugReport server listening on [{0}]:{1}'.format(bindTo, port))
@@ -24,17 +25,31 @@ def server(port, bindTo):
 		report_id = myrandom(10)
 		filename = '/opt/debugserver/reports/' + datetime.date.today().strftime('%Y-%m-%d_') + report_id + '.gz'
 
-		f = open(filename, 'w')
-		while 1:
-			data = conn.recv(BUFFER_SIZE)
-			if not data: break
-			f.write(data) # python will convert \n to os.linesep
-		f.flush()
-		f.close() 
+		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
+			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:
+					conn.close()
+				continue
+			f.flush()
+			# f will be closed automatically by leaving the 'with'-scope
 
 		# send reply to reportee
-		conn.send(report_id)
-		conn.close()
+		response = "%s\r\n" % report_id
+		try:
+			conn.sendall(response)
+		finally:
+			conn.close()
 
 		command = 'echo "'+'new report \\\"{0}\\\" from [{2}]:{3} stored as \\\"{1}\\\""'.format(report_id, filename, addr[0], addr[1])
 		command = command + ' | /usr/local/bin/ff_log_to_bot'