Python socket TCP多线程服务器

'''
Python socket TCP多线程服务器 by 郑瑞国
1、建立网络套接字s
2、绑定地址
3、监听
4、接受客户端连接
5、多线程处理客户端消息
'''
import socket
import threading

s = socket.socket()                                          #1、建立网络套接字s
s.bind(('0.0.0.0',9999))                                     #2、绑定地址
s.listen(5)                                                  #3、监听
def handle(client,addr):
    while True:
        try:
            text = client.recv(1024)
            if not text:
                client.close()
            client.send(text)
            print(addr[0],addr[1],'>>',text.decode())
        except:
            print(addr[0],addr[1],'>>say goodby')
            break
    
while True:
    client,addr=s.accept()                                   #4、接受客户端连接
    threading._start_new_thread(handle,(client,addr))        #5、多线程处理客户端消息

猜你喜欢

转载自blog.csdn.net/zheng_ruiguo/article/details/84804824