标签:else events tor date one blocking ddr try nts
import socket,select s=socket.socket() s.setblocking(False) s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) s.bind((‘127.0.0.1‘,6666)) s.listen() rlist=[s,] wlist=[] erlist=[] while True: rs,ws,er=select.select(rlist,wlist,erlist) print(1) for i in rs: if i is s: con,add=s.accept() rlist.append(con) else: date=i.recv(1024) if not date: rlist.remove(i) print(date.decode()) i.send(date) for i in erlist: print(i)
import socket,select,time s=socket.socket() s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) s.bind((‘127.0.0.1‘,6666)) s.listen() poll=select.poll() poll_dic={s.fileno(): s,} poll.register(s, select.POLLIN | select.POLLERR) while True: try: events=poll.poll() for fd,event in events: print(fd,event) if fd == s.fileno(): con,add=s.accept() poll_dic[con.fileno()] = con poll.register(con,select.POLLIN | select.POLLERR) elif event == select.POLLIN: date = poll_dic[fd].recv(1024) print(date.decode()) if not date: print(‘进来了‘) poll.unregister(fd) poll_dic[fd].close() del poll_dic[fd] except: poll.unregister(s)
import socket,select s=socket.socket() s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) s.bind((‘127.0.0.1‘,6666)) s.listen() poll=select.poll() poll_dict={s.fileno():s} def a(fd): poll.unregister(fd) poll_dict[fd].close() del poll_dict[fd] def main(): poll.register(s,select.POLLIN | select.POLLERR) while True: p=poll.poll() for fd,event in p: if fd == s.fileno(): con, addr = s.accept() poll.register(con, select.POLLIN | select.POLLERR) poll_dict[con.fileno()]=con elif event == select.POLLIN: date=poll_dict[fd].recv(1024) if not date: a(fd) continue print(date.decode(),111) poll_dict[fd].send(date) elif event == select.POLLERR: a(fd) if __name__ == ‘__main__‘: main()
import socket,selectors s=socket.socket() s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) s.bind((‘127.0.0.1‘,6666)) s.listen() epoll=selectors.DefaultSelector() def accept_(s): con,addr=s.accept() epoll.register(con,selectors.EVENT_READ,recv_) def recv_(con): try: date=con.recv(1024) if not date: raise ‘‘ print(date.decode()) con.send(date) except: epoll.unregister(con) con.close() print(‘断开连接‘) def main(): epoll.register(s,selectors.EVENT_READ,accept_) while True: s_eoll=epoll.select() for key,event in s_eoll: ac=key.data #这是文件指针accept_ print(key,‘这是KEY‘) ac(key.fileobj) if __name__ == ‘__main__‘: main()
标签:else events tor date one blocking ddr try nts
原文地址:https://www.cnblogs.com/Skyda/p/9608909.html