客户端:(发送数据)
import socket
import json
import time
class Client(object):
def __init__(self):
self.serverIp = ''
self.serverPort = 9991
self.bufferSize = 1024
self.Arr = (self.serverIp, self.serverPort)
self.client, self.addr = None, None
# 创建socket对象,同时设置通信模式,AF_INET代表IPv4,SOCK_STREAM代表流式socket,使用的是tcp协议
try:
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as e:
print("Failed to create socket. Error: %s"%e)
# 绑定到我们刚刚设置的ip和端口元组,代表我们的服务运行在本机的9999端口上
self.s.bind(self.Arr)
# 开始监听,5位最大挂起的连接数
self.s.listen(5)
def ReceiveMessage(self, predict, args, model, tokenizer):
print('服务启动')
try:
while True:
# 接收来自服务器的数据
print("server waiting")
if self.client == None:
self.client, self.addr = self.s.accept()
print('receive before.')
data = self.client.recv(self.bufferSize)
print('receive after.')
input_Q = str(data, encoding='utf-8')
print('rece: ', input_Q)
pre = predict(args, model, tokenizer, pred_text = input_Q)
print('pre: ', pre)
# 发送给服务器
self.SendMessage(str(pre))
print('继续接收数据.....
')
except socket.error:
self.s.close()
def SendMessage(self, pre):
# 发送数据
client = socket.socket()
# 连接服务器
addr = ('127.0.0.1', 9993) # 用于发送给服务器的端口
client.connect(addr)
print('发送数据....')
# 发送数据
client.send(pre.encode('utf-8'))
client.close()
# client.send(pre.encode('utf-8'))
服务器(接收数据):
import socket
import time
def receiveMessage(s):
print("server waiting") # 接收来自服务器的数据
server, _ = s.accept()
print("接收数据ing.")
data = server.recv(1024)
input_Q = str(data, encoding='utf-8')
print('预测结果:', input_Q)
time.sleep(6)
print('继续发送数据
')
# 用来发送数据给客户端的连接
client = socket.socket()
# 连接服务器
addr = ('127.0.0.1', 9991)
client.connect(addr)
# 用来接收客户端的预测结果
# 创建socket对象,同时设置通信模式,AF_INET代表IPv4,SOCK_STREAM代表流式socket,使用的是tcp协议
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as e:
print("Failed to create socket. Error: %s"%e)
# 绑定到我们刚刚设置的ip和端口元组,代表我们的服务运行在本机的9999端口上
addr2 = ('127.0.0.1', 9993) # 用来接收客户端数据的端口
s.bind(addr2)
# 开始监听,5位最大挂起的连接数
s.listen(5)
server = None
cnt = 0
question = [
'请问羽毛球是冬奥会项目吗?',
'请问冰球是冬奥会项目吗',
'请问台球好玩吗?',
'请问篮球可以锻炼身体吗?',
'请问滑雪是什么运动会项目?',
"中国冬奥会速滑可以夺冠吗?",
'请问小孩喜欢打篮球怎么办?',
'请问姚明是打什么球的?']
while True:
if cnt == len(question):
break
data = question[cnt]
cnt = cnt + 1
# 发送数据
client.send(data.encode('utf-8')) # 发送给客户端
receiveMessage(s)
上面可能有问题
服务器端:
import socket
import json
import time
class Client(object):
def __init__(self):
self.serverIp = ''
self.serverPort = 9991
self.bufferSize = 1024
self.Arr = (self.serverIp, self.serverPort)
self.client, self.addr = None, None
# 创建socket对象,同时设置通信模式,AF_INET代表IPv4,SOCK_STREAM代表流式socket,使用的是tcp协议
try:
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as e:
print("Failed to create socket. Error: %s"%e)
# 绑定到我们刚刚设置的ip和端口元组,代表我们的服务运行在本机的9999端口上
self.s.bind(self.Arr)
# 开始监听,5位最大挂起的连接数
self.s.listen(5)
def ReceiveMessage(self, predict, args, model, tokenizer):
print('服务启动')
try:
while True:
# if self.client == None:
self.client, self.addr = self.s.accept()
data = {}
result = ''
text = ''
try:
# while True:
# t = c.recv(2048).decode('utf-8')
# text += t
# if t[-1] == '}':
# break
# data = json.loads(text)
data = self.client.recv(self.bufferSize).decode('utf-8')
print('question: ', data)
if 'input' in data:
result = str(predict(args, model, tokenizer, pred_text = data))
else:
result = str(predict(args, model, tokenizer, pred_text = data))
except Exception as e:
print('Exception: ', e)
if type(result) == str:
result = result.encode('utf-8')
print('pred: ', result)
self.client.send(result)
print('返回结果成功.')
self.client.close()
# self.client.close()
except socket.error:
self.s.close()
self.client.close()
客户端:
import socket
import time
client = socket.socket()
# 设置服务器端口
addr = ('127.0.0.1', 9991)
client.connect(addr)
# 创建socket对象,同时设置通信模式,AF_INET代表IPv4,SOCK_STREAM代表流式socket,使用的是tcp协议
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as e:
print("Failed to create socket. Error: %s"%e)
# 绑定到我们刚刚设置的ip和端口元组,代表我们的服务运行在本机的9999端口上
addr2 = ('127.0.0.1', 9993) # 用来接收客户端数据的端口
s.bind(addr2)
# 开始监听,5位最大挂起的连接数
s.listen(5)
server = None
# 接收客户端数据
def receiveMessage(question, s):
print("server waiting")
server, _ = s.accept()
print("接收数据ing.")
data = server.recv(1024)
input_Q = str(data, encoding='utf-8')
print(question, ' 预测结果:', input_Q)
time.sleep(6)
print('继续发送数据
')
cnt = 0
question = [
'请问小孩咳嗽总是不好怎么办?',
'请问中国上一届比赛冰球是什么名次?',
'请问羽毛球是冬奥会项目吗?',
'请问冰球是冬奥会项目吗',
'请问台球好玩吗?',
'请问篮球可以锻炼身体吗?',
'请问滑雪是什么运动会项目?',
"中国冬奥会速滑可以夺冠吗?",
'请问小孩喜欢打篮球怎么办?',
'请问姚明是打什么球的?']
while True:
if cnt == len(question):
break
data = question[cnt]
cnt = cnt + 1
# 发送数据
print('发送数据ing....')
client.send(data.encode('utf-8')) # 发送给客户端
# receiveMessage(question[cnt-1], s) # 接收客户端返回的预测结果
server_reply = client.recv(1024)
input_Q = str(server_reply, encoding='utf-8')
print(question[cnt-1], ' 预测结果:', input_Q)
time.sleep(6)
print('继续发送数据
')