标签:style os io 使用 ar for sp on c
# -*- coding: utf-8 -*-
# python:2.x
__author__ = ‘Administrator‘
#作用:等待输入或者输出通道已经准备就绪的通知
"""
允许访问特定的平台i/o监视函数,最可移植接口是POSIX函数select()
unix和windwods提示了这2个函数,这个模块可以实现异步通信,select模块包含了poll()和select(),select()原型是(rlist,wlist,xlist[,timeout])
rlist等待读取对象,wlist等待定稿对象,xlist等待异常对象,最后一个可选对象,指定等待时间,单位是s,select()方法返回值准备好的对象是三元组,若在timeout时间内,没有对象准备好,那么返回值是空的列表
"""
#select服务器
from socket import *
server=socket(AF_INET,SOCK_STREAM)
server.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)#socket.setsockopt(level, optname, value)
#Set the value of the given socket option (see the Unix manual page setsockopt(2)).
# The needed symbolic constants are defined in the socket module (SO_* etc.). The value can be an integer or a string representing a buffer.
# In the latter case it is up to
# the caller to ensure that the string contains the proper bits (see the optional built-in module
# struct for a way to encode C structures as strings).
server.bind((‘‘,10000))
inputs=[server]
from select import *
while True:
re1,ws,es=select(inputs,[],[])
for r in re1:
if r in server:
clientsock,clientaddr=r.accept()
inputs.append(clientsock)
else:
data=r.recv(1024)
if not data:
inputs.remove(r)
else:
print data
# -*- coding: utf-8 -*-
# python:2.x
__author__ = ‘Administrator‘
#客户端
from socket import *
host=‘127.0.0.1‘
port=10000
s=socket(AF_INET,SOCK_STREAM)
s.connect((host,port))
s.send(‘hello world!‘)
s.close()
标签:style os io 使用 ar for sp on c
原文地址:http://www.cnblogs.com/mhxy13867806343/p/3956741.html