forked from Tencent/NeuralNLP-NeuralClassifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsock_server.py
More file actions
67 lines (56 loc) · 2.03 KB
/
sock_server.py
File metadata and controls
67 lines (56 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import socket
import predictor
import os, sys
service = ('localhost', 4444)
data=''
while True:
try:
if __name__ == "__main__":
print('loading model')
# load model config
# load model entity
pdt = predictor.Predictor(sys.argv[1])
# load infer batch(tied with cpu)i
# create a socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind
#sock.settimeout(15)
sock.bind(service)
# socket, start to listen
sock.listen(64)
while True:
# receieve data#print('reading data')
print('listening')
con, meat = sock.accept()
data=''
input_texts = []
while True:
buff=con.recv(4096)
if buff:
#print('\n',buff[-4:], '\n')
if buff[-4:] == b'\x02\x02\x02\x02':
data+=buff[:-4].decode("utf-8", "ignore")
break
data+=buff.decode("utf-8", "ignore")
#con.sendall(buff)
else:
break
tag=False
data=data.split('\n')
if len(data) < 0:
con.close()
continue
elif len(data) == 1:
data = [data[0], data[0]]
tag=True
for line in data:
input_texts.append(line.strip("\n"))
print('predicting')
predict_label_namez=pdt.predict_batch(input_texts)
if tag:
con.sendall([predict_label_namez[0]].__str__().encode('utf-8'))
else:
con.sendall(predict_label_namez.__str__().encode('utf-8'))
con.close()
except Exception as E:
print('socket server:', data, E, 'restarting')