被动套接字和新建出套接字的区别

day_6.14py网络编程-LMLPHP

单任务,单线程,非阻塞!!!!服务器!

#2018-6-14 16:51:25
#!--*--coding:utf-8 --*-- '''
单进程,单任务 少的可以,多的受不了
2018-6-14 18:44:252
'''
from socket import *
#1.创建socket
serSocket = socket(AF_INET, SOCK_STREAM) #2.绑定本地ip以及port
localAddr = ('', 7788)
serSocket.bind(localAddr) #3:让这个socket 变为非阻塞
# serSocket.setblocking(False) #4.将socket变为监听(被动)套接字
serSocket.listen(5) #用来保存所有已经连接的客户端信息
clientAddrList =[] while True:
#等待一个新的客户端的到来(即完成三次握手的客户端)
try:
clientSocket,clientAddr = serSocket.accept()
except:
pass
else:
print("一个新的客户端到来:%s"%str(clientAddr))
clientSocket.setblocking(False)
clentAddrList.append((clientSocket,clientAddr)) for clientSocket,clientAddr in clientAddrList:
try:
recvData = clientScoket.recv(1024)
except:
pass
else:
if len(recvData)>0:
print("%s:%s"%(str(clientAddr),recvData))
else:
clientScoket.close()
clientAddrList.remove((clientScoket,clientAddr))
print("%s 已下线"%str(clientAddr))
04-15 08:02