基于python select.select模块通信的实例讲解

要理解select.select模块其实主要就是要理解它的参数, 以及其三个返回值。
select()方法接收并监控3个通信列表, 第一个是所有的输入的data,就是指外部发过来的数据,第2个是监控和接收所有要发出去的data(outgoing data),第3个监控错误信息在网上一直在找这个select.select的参数解释, 但实在是没有, 哎...自己硬着头皮分析了一下。    
readable, writable, exceptional = select.select(inputs, outputs, inputs)
第一个参数就是服务器端的socket, 第二个是我们在运行过程中存储的客户端的socket, 第三个存储错误信息。
重点是在返回值, 第一个返回的是可读的list, 第二个存储的是可写的list, 第三个存储的是错误信息的list。
这个也不必深究, 看看代码自己分析下就能有大概理解。
网上所有关于select.select的代码都是差不多的, 但是有些不能运行, 或是不全。我自己重新写了一份能运行的程序, 做了很多注释, 好好看看就能搞懂
服务器端:    
# coding: utf-8
import select
import socket
import Queue
from time import sleep
 
 
# Create a TCP/IP
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking(False)
 
# Bind the socket to the port
server_address = ('localhost', 8090)
print ('starting up on %s port %s' % server_address)
server.bind(server_address)
 
# Listen for incoming connections
server.listen(5)
 
# Sockets from which we expect to read
inputs = [server]
 
# Sockets to which we expect to write
# 处理要发送的消息
outputs = []
 
# Outgoing message queues (socket: Queue)
message_queues = {}
 
while inputs:
  # Wait for at least one of the sockets to be ready for processing
  print ('waiting for the next event')
  # 开始select 监听, 对input_list 中的服务器端server 进行监听
  # 一旦调用socket的send, recv函数,将会再次调用此模块
  readable, writable, exceptional = select.select(inputs, outputs, inputs)
 
  # Handle inputs
  # 循环判断是否有客户端连接进来, 当有客户端连接进来时select 将触发
  for s in readable:
    # 判断当前触发的是不是服务端对象, 当触发的对象是服务端对象时,说明有新客户端连接进来了
    # 表示有新用户来连接
    if s is server:
      # A "readable" socket is ready to accept a connection
      connection, client_address = s.accept()
      print ('connection from', client_address)
      # this is connection not server
      connection.setblocking(0)
      # 将客户端对象也加入到监听的列表中, 当客户端发送消息时 select 将触发
      inputs.append(connection)
 
      # Give the connection a queue for data we want to send
      # 为连接的客户端单独创建一个消息队列,用来保存客户端发送的消息
      message_queues[connection] = Queue.Queue()
    else:
      # 有老用户发消息, 处理接受
      # 由于客户端连接进来时服务端接收客户端连接请求,将客户端加入到了监听列表中(input_list), 客户端发送消息将触发
      # 所以判断是否是客户端对象触发
      data = s.recv(1024)
      # 客户端未断开
      if data != '':
        # A readable client socket has data
        print ('received "%s" from %s' % (data, s.getpeername()))
        # 将收到的消息放入到相对应的socket客户端的消息队列中
        message_queues[s].put(data)
        # Add output channel for response
        # 将需要进行回复操作socket放到output 列表中, 让select监听
        if s not in outputs:
          outputs.append(s)
      else:
        # 客户端断开了连接, 将客户端的监听从input列表中移除
        # Interpret empty result as closed connection
        print ('closing', client_address)
        # Stop listening for input on the connection
        if s in outputs:
          outputs.remove(s)
        inputs.remove(s)
        s.close()
 
        # Remove message queue