代码
# coding:utf-
from socket import * # .创建服务器socket
sock = socket(AF_INET, SOCK_STREAM) # .绑定主机和端口
addr = ('', ) #
sock.bind(addr) # . 设置最大监听数目,并发
sock.listen() # . 设置成非阻塞
sock.setblocking(False)
# 保存客户端socket
clientAddrList = []
# print(sock.) while :
try:
clientSocket, clientAddr = sock.accept()
except:
pass
else:
print("一个新客户端到来:%s" % str(clientAddr))
clientSocket.setblocking(False)
clientAddrList.append((clientSocket, clientAddr)) for clientSocket, clientAddr in clientAddrList:
try:
recvData = clientSocket.recv()
except:
pass
else:
if len(recvData) > :
print("%s:%s" % (str(clientAddr), recvData))
else:
clientSocket.close()
clientAddrList.remove((clientSocket, clientAddr))
print("%s 已经下线" % str(clientAddr))