mirror of
https://github.com/DJ2LS/FreeDATA
synced 2024-05-14 08:04:33 +00:00
improved socket
only read one line to avoid decoding errors
This commit is contained in:
parent
b813e8562c
commit
98f52dfa2e
|
@ -64,12 +64,15 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
|
||||||
|
|
||||||
# we need to loop through buffer until end of chunk is reached or timeout occured
|
# we need to loop through buffer until end of chunk is reached or timeout occured
|
||||||
while True and socketTimeout > time.time():
|
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
|
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
|
break
|
||||||
data = data[:-1] # remove b'\n'
|
data = data[:-1] # remove b'\n'
|
||||||
data = str(data, 'utf-8')
|
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)
|
#print(data)
|
||||||
|
|
||||||
if len(data) > 0:
|
if len(data) > 0:
|
||||||
|
|
|
@ -57,12 +57,15 @@ class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
|
||||||
# we need to loop through buffer until end of chunk is reached or timeout occured
|
# we need to loop through buffer until end of chunk is reached or timeout occured
|
||||||
while True and socketTimeout > time.time():
|
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
|
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
|
break
|
||||||
data = data[:-1] # remove b'\n'
|
data = data[:-1] # remove b'\n'
|
||||||
data = str(data, 'utf-8')
|
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)
|
#print(data)
|
||||||
|
|
||||||
if len(data) > 0:
|
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]
|
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
|
||||||
print(exc_type, fname, exc_tb.tb_lineno)
|
print(exc_type, fname, exc_tb.tb_lineno)
|
||||||
print("############ END OF ERROR #######################")
|
print("############ END OF ERROR #######################")
|
||||||
|
|
||||||
print("reset of connection...")
|
print("reset of connection...")
|
||||||
socketTimeout = 0
|
socketTimeout = 0
|
||||||
print("Client disconnected...")
|
print("Client disconnected...")
|
||||||
|
|
Loading…
Reference in a new issue