Use the Select module in the Python module to implement web chat room functionality
Select module
The Select module in Python focuses on I/O multiplexing, provides a select poll Epoll three methods (where the latter two are available in Linux, Windows supports select only), and also provides the Kqueue method (FreeBSD system)
Parameters: accepts four parameters (the first three must be)
Rlist:wait until ready for reading
Wlist:wait until ready for writing
Xlist:wait for an "exceptional condition"
Timeout: Timeout period
Select method:
Every call to Slect will copy all of the FD to the kernel space ( copy every time ), resulting in a decrease in efficiency
Every call to Slect will copy all of the FD to the kernel space (copy every time), resulting in a decrease in efficiency
The implementation of the listener is to determine if there is data access by iterating through all of the FD, which consumes more time.
Maximum number of connections (number of file descriptors placed in input 1024)
Pull method:
There is no limit to the maximum number of connections, except in the case of select. Use less
Epull Method:
Internally implemented by 3 functions (select is one of them)
First function:
Create Epoll handle, copy all FD to kernel space, only need to copy once
Second function: callback function
When a function or action is successfully completed, a function is automatically triggered to bind a callback function for all FD, and once the data is accessed, the callback function is triggered, and the callback function puts FD into the list. (As long as there is activity, put FD into the list, dynamic monitoring) This improves efficiency. Example: handing paper
A third function that determines whether the linked list is empty
Server-side code
#/usr/bin/env python#-*- coding:utf-8 -*-import socketimport select# Package Class selectserver (object): # defines the main function def __init__ (self, host, port, backlog): self.host = host self.port = port self.address = (Host, port) Self.backlog = backlog self.server = none self.socketlist = list () def _initsocket (self): self.server = socket.socket ( Socket.af_inet, socket. SOCK_STREAM) self.server.bind (self.address) &nbsP; self.server.listen (Self.backlog) self.socketlist.append (Self.server) print ("Chat room has start! ") while 1: rlist, wlist, elist = select.select (self.socketList, [], []) for r in rlist : if r == self.server: serverconn, clienaddr = self.server.accept () Self.sOcketlist.append (Serverconn) print ("{0} entered the room". Format (CLIENADDR)) self.broadcast (R, "{0} entered the room". Format (CLIENADDR)) else: try:               DATA = R.RECV ( 2048) if data: print ("{0}: {1}". Format ( Clienaddr, data)) self.broadcast (r, "{0 }: {1} ". Format (clienaddr, data)) except Exception as e: self.broadcast (r, "{0} offline". Format (CLIENADDR)) print ("{ 0} Offline ". Format (CLIENADDR)) &Nbsp; r.close () self.socketlist.remove (R) self.server.close () # define broadcast functions def broadcast (self, r, data): for i in self.socketList: if i != r and i != self.server: try: i.sendall (data) except: &nbSp; i.close () Self.socketList.remove (i) # defines the main function def main (): selectserver = Selectserver (host= "192.168.154.131", port=9999, backlog=5) selectserver._ Initsocket () if __name__ == ' __main__ ': main ()
Client-side code
#/usr/bin/env python#-*- coding:utf-8 -*-import socket, select, string, sysimport time# main functionif __name__ == "__main__": host = "192.168.154.131" port = 9999 s = socket.socket (Socket.af_inet, socket. SOCK_STREAM) s.settimeout (2) try: s.connect ((host, port)) except: print (' Unable to connect ') sys.exit () print (' connected to remote host. start sending Messages ') while 1: rlist = [sys.stdin, s] read_list, Write_list, error_list = select.select (rlist, [], []) for sock in read_list: if sock == s:       DATA = SOCK.RECV (2048) if not data: continue else: Sys.stdout.write (data) else: &Nbsp; msg = raw_input ("I say: ") s.sendall (msg)
Python implementation web chat room