select_socket_server简单实例
import select,socket,queue

server=socket.socket()

server.bind(('localhost',9000))
server.listen(1000)

server.setblocking(False)#设置非阻塞模式

inputs=[server,]
#inputs=[server,conn] #[conn,]
#inputs=[server,conn,conn2] #[conn2,]
outputs=[]
while True:
    readable,writeable,exceptional=select.select(inputs,outputs,inputs)#select监测server连接  返回三个数据
    print(readable,writeable,exceptional)
    for r in readable:
        if r is server:#代表来了一个新连接 下一次select
            conn,addr=server.accept()
            print("来了一个新连接:",addr)
            inputs.append(conn)#是因为这个新建立的连接还没发数据过来,现在就接收的话程序就报错了,
            #所以要想实现这个客户端发数据来时server端能知道,就需要让select在监测这个conn
        else:#如果不是新连接,是之前那个conn 就接收数据 下一次select
            data=r.recv(1024)
            print("收到数据:",data)
            data1=(str(data.decode("utf-8")).upper()).encode()
            r.send(data1)
            print("send done...")

 

 

有队列的select_socket_server实例
import select,socket,queue

server=socket.socket()

server.bind(('localhost',9000))
server.listen(1000)

server.setblocking(False)#设置非阻塞模式

msg_dic={}

inputs=[server,]
#inputs=[server,conn] #[conn,]
#inputs=[server,conn,conn2] #[conn2,]
outputs=[] #r
#outputs=[r1,]
while True:
    readable,writeable,exceptional=select.select(inputs,outputs,inputs)#select监测server连接  返回三个数据
    print(readable,writeable,exceptional) #outputs 放啥下一次就出啥 下一次循环的时候就返回了
    for r in readable:
        if r is server:#代表来了一个新连接 下一次select
            conn,addr=server.accept()
            print("来了一个新连接:",addr)
            inputs.append(conn)#是因为这个新建立的连接还没发数据过来,现在就接收的话程序就报错了,
            #所以要想实现这个客户端发数据来时server端能知道,就需要让select在监测这个conn
            msg_dic[conn]=queue.Queue()#初始化一个队列,后面存要返回给这个客户端的数据

        else:#如果不是新连接,是之前那个conn 就接收数据 下一次select
            data=r.recv(1024)
            print("收到数据:",data)
            msg_dic[r].put(data) #往队列里面扔数据 等于字典里添加values

            outputs.append(r) #放入返回的连接队列里
            #r.send(data)
            #print("send done...")

    for w in writeable:#要返回给客户端的连接列表
        data_to_client=msg_dic[w].get() #返回给这个客户端的数据 取队列里面的数据 相当于取字典里面的values
        w.send(data_to_client)#返回给客户端的源数据

        outputs.remove(w)#确保下次循环的时候writeable,不返回已经处理完的连接

    for e in exceptional:#连接已经断开,异常处理
        if e in outputs:
            outputs.remove(e)#清理已经断开的连接

        inputs.remove(e)#清理已经断开的连接

        del msg_dic[e]#清理已经断开的连接

 

 

支持大并发的selectors模块
import selectors
import socket

sel = selectors.DefaultSelector()


def accept(sock, mask):#相当于server.accept()
    conn, addr = sock.accept()  # Should be ready
    print('accepted', conn, 'from', addr)
    conn.setblocking(False)
    sel.register(conn, selectors.EVENT_READ, read)#新连接注册read回调函数


def read(conn, mask):#操作
    data = conn.recv(1024)  # Should be ready
    if data:
        print('echoing', repr(data), 'to', conn,mask)
        conn.send(data)  # Hope it won't block
    else:
        print('closing', conn)
        sel.unregister(conn)
        conn.close()


sock = socket.socket()
sock.bind(('localhost', 10000))
sock.listen(1000)
sock.setblocking(False)
sel.register(sock, selectors.EVENT_READ, accept)

while True:
    events = sel.select() #看系统支持 select或 epool 默认阻塞 有活动连接就返回活动的连接列表
    for key, mask in events:
        callback = key.data #accept
        callback(key.fileobj, mask) #key.fileobj=文件句柄

 


多路连接socket_client
#_*_coding:utf-8_*_
__author__ = 'Alex Li'


import socket
import sys

messages = [ b'This is the message. ',
             b'It will be sent ',
             b'in parts.',
             ]
server_address = ('localhost',10000)

# Create a TCP/IP socket
socks = [ socket.socket(socket.AF_INET, socket.SOCK_STREAM) for i in range(100)
          ]

# Connect the socket to the port where the server is listening
print('connecting to %s port %s' % server_address)
for s in socks:
    s.connect(server_address)

for message in messages:

    # Send messages on both sockets
    for s in socks:
        print('%s: sending "%s"' % (s.getsockname(), message) )
        s.send(message)

    # Read responses on both sockets
    for s in socks:
        data = s.recv(1024)
        print( '%s: received "%s"' % (s.getsockname(), data) )
        if not data:
            print( 'closing socket', s.getsockname() )