使用多线程实现多个客户端与服务器通信
#服务器端
import socket
from threading import Thread
class CoversationThread(Thread):
"""在子线程中处理不同的客户端会话"""
"""
python中可以在函数参数的后面加一个冒号,来对参数的类型进行说明
"""
def __init__(self, conversation:socket.socket, address):
super().__init__()
self.conversation = conversation
self.address = address
def run(self):
while True:
self.conversation.send('你好!'.encode())
print(self.address,self.conversation.recv(1024).decode(encoding='utf-8'))
if __name__ == '__main__':
server = socket.socket()
server.bind(('10.7.181.117', 8080))
server.listen(512)
while True:
conversation, address = server.accept()
t = CoversationThread(conversation, address)
t.start()
#客户端
import socket
if __name__ == '__main__':
client = socket.socket()
client.connect(('10.7.181.117', 8080))
while True:
print(client.recv(1024).decode(encoding='utf-8'))
message = input('>>>')
client.send(message.encode())
网友评论