写一个基于TCP协议套接字,服务端实现接收客户端的连接并发。
# client
import socket
client = socket.socket()
client.connect(
('127.0.0.1', 9527)
)
while True:
msg = input('>>: ').strip()
if not msg:
continue
client.send(msg.encode('utf-8'))
data = client.recv(1024)
print(data)
# server
import socket
from threading import Thread
import threading
server = socket.socket()
server.bind(
('127.0.0.1', 9527)
)
server.listen(3)
def action(conn):
while True:
data = conn.recv(1024)
print(data)
conn.send(data.upper())
if __name__ == '__main__':
while True:
conn, addr = server.accept()
p = threading.Thread(target=action, args=(conn,))
p.start()