Python网络编程基础笔记-select实现非阻塞socket

1.使用select实现非阻塞socket
               
               
                # -*- coding: cp936 -*-
"""
非阻塞socket的使用(此程序在ubuntu linux和windows xp上测试,Windows可以支持select.select)
监控socket的三个list:in/out/err
程序以5000ms的时间长度为间隔,如果有客户端的请求,接收连接并进行显示;如果没有的话,
每隔5000ms显示一次"no data coming"
"""
import socket,select
host = ""
port = 50000
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((host,port))
s.listen(5)
while 1:
    infds,outfds,errfds = select.select([s,],[],[],5)
    # 如果infds状态改变,进行处理,否则不予理会
    if len(infds) != 0:
        clientsock,clientaddr = s.accept()
        buf = clientsock.recv(8196)
        if len(buf) != 0:
                print (buf)
        clientsock.close()
    print "no data coming"
2.客户端程序与poll客户端基本一致,仅用来发送一字符串
# -*- coding: cp936 -*-
"""
向服务器端发送字符串
"""
import socket,select
host = "localhost"
port = 50000
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((host,port))
s.send("coming from select client")
s.close()