#-*-coding:utf-8-*-
from socket import *
import select
import sys
s = socket(AF_INET,SOCK_STREAM)
#s.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
addr = ("",7878)
s.bind(addr)
s.listen(5)
input = [s,sys.stdin]#sys检测键盘输入
runing = True
while True:
readable,writeable,exceptional = select.select(input,[],[])
for sock in readable:
if sock == s:
newClient,clientAddr = s.accept()
input.append(newClient)
elif sock == sys.stdin:
cmd = sys.stdin.readline()
running = False
break
else:
recvData = newClient.recv(1024)
if recvData:
sock.send(recvData)
else:
input.remove(sock)
sock.close()
if not runing:
break
s.close()
python核心编程:select版-TCP服务器
最新推荐文章于 2024-08-18 21:05:00 发布