improved socket

only read one line to avoid decoding errors
This commit is contained in:
DJ2LS 2021-09-24 10:09:47 +02:00
parent b813e8562c
commit 98f52dfa2e
2 changed files with 11 additions and 4 deletions

View file

@ -64,12 +64,15 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
# we need to loop through buffer until end of chunk is reached or timeout occured
while True and socketTimeout > time.time():
chunk = self.request.recv(1024) # .strip()
chunk = self.request.recv(64) # we keep amount of bytes short
data += chunk
if chunk.startswith(b'{') and chunk.endswith(b'}\n'):
if chunk.endswith(b'}\n') or chunk.endswith(b'}'): # or chunk.endswith(b'\n'):
break
data = data[:-1] # remove b'\n'
data = str(data, 'utf-8')
# only read first line of string. multiple lines will cause an json error
# this occurs possibly, if we are getting data too fast
data = data.splitlines()[0]
#print(data)
if len(data) > 0:

View file

@ -57,12 +57,15 @@ class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
# we need to loop through buffer until end of chunk is reached or timeout occured
while True and socketTimeout > time.time():
chunk = self.request.recv(1024) # .strip()
chunk = self.request.recv(64) # we keep amount of bytes short
data += chunk
if chunk.endswith(b'}\n'):# or chunk.endswith(b'}') or chunk.endswith(b'\n'):
if chunk.endswith(b'}\n') or chunk.endswith(b'}'): # or chunk.endswith(b'\n'):
break
data = data[:-1] # remove b'\n'
data = str(data, 'utf-8')
# only read first line of string. multiple lines will cause an json error
# this occurs possibly, if we are getting data too fast
data = data.splitlines()[0]
#print(data)
if len(data) > 0:
@ -242,6 +245,7 @@ class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
print("############ END OF ERROR #######################")
print("reset of connection...")
socketTimeout = 0
print("Client disconnected...")