bug: not head but decode as head
This commit is contained in:
@@ -70,32 +70,34 @@ class Netrecv:
|
|||||||
def process_connection(self, conn, addr):
|
def process_connection(self, conn, addr):
|
||||||
print('Connection accpet %s' % str(addr))
|
print('Connection accpet %s' % str(addr))
|
||||||
data = b''
|
data = b''
|
||||||
|
need_data = False
|
||||||
while True:
|
while True:
|
||||||
new_data = conn.recv(RECV_BUFF) # here needs to check whether the package is continued
|
new_data = conn.recv(RECV_BUFF) # here needs to check whether the package is continued
|
||||||
if not new_data:
|
if not new_data:
|
||||||
conn.close()
|
conn.close()
|
||||||
return
|
return
|
||||||
data += new_data
|
data += new_data
|
||||||
while True:
|
while True: # process sticky package
|
||||||
dp = Datapack(check_head=False)
|
dp = Datapack(check_head=False)
|
||||||
dp.encode_data = data
|
dp.encode_data = data
|
||||||
try:
|
try:
|
||||||
|
if not need_data:
|
||||||
data = dp.decode(only_head=True)
|
data = dp.decode(only_head=True)
|
||||||
except Exception as e:
|
except Exception as e: # check head
|
||||||
print('Decode error %s: %s' % (type(e), str(e)))
|
print('Decode error %s: %s' % (type(e), str(e)))
|
||||||
print('Stop and start to receive more data')
|
print('Stop and start to receive more data')
|
||||||
break
|
break
|
||||||
length = int(dp.head['length'])
|
length = int(dp.head['length'])
|
||||||
if length > len(data):
|
if length > len(data): # check body
|
||||||
print('No enougth data, stop and start to receive')
|
print('No enougth data, stop and start to receive')
|
||||||
|
need_data = True
|
||||||
break
|
break
|
||||||
|
elif length == len(data):
|
||||||
|
need_data = False
|
||||||
dp.body = data[:length] # get the body
|
dp.body = data[:length] # get the body
|
||||||
data = data[length:]
|
data = data[length:]
|
||||||
dp.encode()
|
dp.encode()
|
||||||
print(dp.body)
|
|
||||||
print('---------------\n'+dp.encode_data.decode()+'\n---------------')
|
print('---------------\n'+dp.encode_data.decode()+'\n---------------')
|
||||||
if not data:
|
|
||||||
break
|
|
||||||
|
|
||||||
|
|
||||||
class Netlist: # contain net list and network controller
|
class Netlist: # contain net list and network controller
|
||||||
|
|||||||
32
test_file.py
32
test_file.py
@@ -1,28 +1,34 @@
|
|||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
|
|
||||||
data = '''post log msw/1.0
|
data = '''file log msw/1.0
|
||||||
from: network
|
from: network
|
||||||
flag: abcdefgh
|
flag: abcdefgh
|
||||||
num: 1/2
|
num: 1/1
|
||||||
lengt'''
|
lengt'''
|
||||||
|
|
||||||
data2 = '''h: '''
|
data2 = '''h: 9
|
||||||
|
|
||||||
data3 ='''3
|
123'''
|
||||||
|
|
||||||
|
data3 ='''456789'''
|
||||||
|
|
||||||
|
data4 = '''post log msw/1.1
|
||||||
|
from: network
|
||||||
|
flag: 12345678
|
||||||
|
num: 1/1
|
||||||
|
length: 3
|
||||||
|
|
||||||
abc'''
|
abc'''
|
||||||
|
|
||||||
data = data.encode()
|
data_list = [data, data2, data3,data4]
|
||||||
data2 = data2.encode()
|
code_list = []
|
||||||
data3 = data3.encode()
|
for i in data_list:
|
||||||
|
code_list.append(i.encode())
|
||||||
|
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
s.connect(('127.0.0.1', 3900))
|
s.connect(('127.0.0.1', 3900))
|
||||||
|
|
||||||
s.sendall(data)
|
for i in code_list:
|
||||||
time.sleep(1)
|
s.sendall(i)
|
||||||
s.sendall(data2)
|
time.sleep(1)
|
||||||
time.sleep(1)
|
|
||||||
s.sendall(data3)
|
|
||||||
s.close()
|
|
||||||
Reference in New Issue
Block a user