16 Star 45 Fork 46

现任明教教主-乾颐堂/qytang_Python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
2016.1.16 select多路复用服务器 1006 Bytes
一键复制 编辑 原始数据 按行查看 历史
现任明教教主-乾颐堂 提交于 2016-01-16 11:15 . new file
import sys, time
from select import select
from socket import *
def now(): return time.ctime(time.time())
myHost = ''
myPort = 50007
if len(sys.argv) == 3:
myHost, myPort = sys.argv[1:]
numPortSocks = 2
mainsocks, readsocks, writesocks = [], [], []
for i in range(numPortSocks):
portsock = socket(AF_INET, SOCK_STREAM)
portsock.bind((myHost, myPort))
portsock.listen(5)
mainsocks.append(portsock)
readsocks.append(portsock)
myPort += 1
print('select-server loop starting')
while True:
readables, writeables, exceptions = select(readsocks, writesocks, [])
for sockobj in readables:
if sockobj in mainsocks:
newsock, address = sockobj.accept()
print('Connect:', address, id(newsock))
readsocks.append(newsock)
else:
data = sockobj.recv(1024)
print('\tgot', data, 'on', id(sockobj))
if not data:
sockobj.close()
readsocks.remove(sockobj)
else:
reply = 'Echo=>%s at %s' % (data, now())
sockobj.send(reply.encode())
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/qytang/qytang_Python.git
git@gitee.com:qytang/qytang_Python.git
qytang
qytang_Python
qytang_Python
master

搜索帮助