服务端
from gevent import monkey;monkey.patch_all()
import socket
import struct, json
from gevent import spawn
def task(conn, c_add):
'''
在子线程内,实现对应客户端等待输入和数据处理
:param conn:
:return:
'''
while True:
try:
b_header = conn.recv(struct.unpack('i', conn.recv(4))[0])
header = json.loads(b_header.decode('utf-8'))
user = header['user']
msg = header['msg']
print('%s %s : %s' % (c_add, user, msg))
except ConnectionResetError:
break
except struct.error:
break
def server(IP, PORT, BACKLOG=4):
ADD = (IP, PORT)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADD)
server.listen(BACKLOG)
# 等待建立链接,并且进行通信
while True:
conn, c_add = server.accept()
# 异步提交连接,执行task
spawn(task, conn, c_add)
if __name__ == '__main__':
# 异步提交
g1 = spawn(server, '127.0.0.1', 8080,)
# 主进程等待协程执行结束
g1.join()
客户端
import socket, struct, json
from threading import Thread,current_thread
def client(IP, PORT):
S_ADD = (IP, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(S_ADD)
while True:
msg = 'hello,world'
if msg == 'q':
break
header = {
'user': current_thread().name,
'msg': msg
}
j_header = json.dumps(header)
b_header = j_header.encode('utf-8')
s_header = struct.pack('i', len(b_header))
client.send(s_header)
client.send(b_header)
if __name__ == '__main__':
# 开启线程同时访问服务端,模拟测压
for i in range(100):
t = Thread(target=client, args=('127.0.0.1', 8080,))
t.start()
执行结果
………………
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59378) Thread-7 : hello,world
('127.0.0.1', 59378) Thread-7 : hello,world
('127.0.0.1', 59380) Thread-6 : hello,world
('127.0.0.1', 59380) Thread-6 : hello,world
('127.0.0.1', 59380) Thread-6 : hello,world
('127.0.0.1', 59380) Thread-6 : hello,world
('127.0.0.1', 59380) Thread-6 : hello,world
('127.0.0.1', 59387) Thread-11 : hello,world
('127.0.0.1', 59387) Thread-11 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59380) Thread-6 : hello,world
('127.0.0.1', 59380) Thread-6 : hello,world
('127.0.0.1', 59380) Thread-6 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59380) Thread-6 : hello,world
('127.0.0.1', 59380) Thread-6 : hello,world
('127.0.0.1', 59387) Thread-11 : hello,world
('127.0.0.1', 59387) Thread-11 : hello,world
('127.0.0.1', 59387) Thread-11 : hello,world
('127.0.0.1', 59387) Thread-11 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59387) Thread-11 : hello,world
('127.0.0.1', 59387) Thread-11 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59387) Thread-11 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59387) Thread-11 : hello,world
('127.0.0.1', 59387) Thread-11 : hello,world
('127.0.0.1', 59383) Thread-3 : hello,world
('127.0.0.1', 59387) Thread-11 : hello,world
('127.0.0.1', 59387) Thread-11 : hello,world
………………