多线程服务器
import socket
from threading import Thread
def handle_client(client_sock, client_addr):
while True:
recv_data = client_sock.recv(1024)
if len(recv_data) > 0:
print('recv[%s]:%s' % (str(client_addr), recv_data.decode()))
else:
print('[%s]客户端已经关闭' % str(client_addr))
break
client_sock.close()
def main():
server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
address = ('', 8000)
server_sock.bind(address)
server_sock.listen(128)
while True:
print('-----主线程,,等待新客户端的到来------')
client_sock, client_addr = server_sock.accept()
print('-----主线程,,接下来创建一个新的线程负责数据处理[%s]-----' % str(client_addr))
thread = Thread(target=handle_client, args=(client_sock, client_addr))
thread.start()
server_sock.close()
if __name__ == '__main__':
main()