Python Basic Tutorial Summary 15--5 Virtual Tea Party

Source: Internet
Author: User

Chat server:

The server can accept multiple connections from different users;

Allows simultaneous (parallel) operation of the user;

Can explain commands, for example, say or logout;

Easy to expand

1. Initial implementation

1.1 Charserver Class

There is a problem ........

#servers that can accept connections fromAsyncoreImportDispatcherImportAsyncore,socketclassChatserver (Dispatcher):defhandle_accept (self): conn.addr=self.accept ()Print 'Connection attempt from', addr[0] #客户端的Ip地址s=chatserver () s.create_socket (socket.af_inet, socket. SOCK_STREAM)#Creating SocketsS.bind ((' ', 5005))#binds a socket to a port, an empty string (hostname), which means the local host (that is, all local interfaces) with a port number of 5005S.listen (5)#告诉服务器要监听连接, and specify 5 connected things for the agentAsyncore.loop () #启动服务器, cyclic monitoring
#basic server with clean-up capability fromAsyncoreImportDispatcherImportsocket, Asyncoreport=5005classChatserver (Dispatcher):def __init__(Self,port): Dispatcher.__init__(self) self.create_socket (socket.af_inet, socket. SOCK_STREAM) self.set_reuse_addr () Self.bind ((' ', Port)) Self.listen (5)    defhandle_accept (self): conn, addr=self.accept ()Print 'Connection attempt from', Addr[0]if __name__=="__main__": S=chatserver (PORT)Try: Asyncore.loop ()exceptKeyboardinterrupt:Pass

1.2 Charsession Class

1) The Set_terminator method is used to set the line termination object to the "\ r \ n" typically used as a terminator in the network protocol.

2) The Chatsession object will save the currently read data as a string list. However, when more data is read, Collect_incoming_data is automatically called to append the newly read data to the list.

3) The Found_terminiator method is called when it reads to the terminating object

4) Chatserver Save reply list

5) Charserver's Handle_accept method now creates a new Chatsession object and appends it to the session list

#server programs with the Chatsession class fromAsyncoreImportDispatcher fromAsynchatImportAsync_chatImportSocket,asyncoreport=5005classchatsession (async_chat):def __init__(Self,sock): Async_chat.__init__(Self,sock) self.set_terminator ("\ r \ n") Self.data=[]    defCollect_incoming_data (self,data): self.data.append (data)defFound_terminator (self): line=' '. Join (self.data) Self.data=[]        #process the row data        Print LineclassChatserver (Dispatcher):def __init__(self,port): Diapatcher.__init__(self) self.create_socket (socket.af_inet,socket. SOCK_STREAM) self.set_reuse_addr () Self.bind ((' '. Port)) Self.listen (5) Self.sessions=[]    defhandle_accept (self): conn,addr=self.accept () self.sessions.append (chatsession (COM))if __name__=='__main__': S=chatserver (PORT)Try: Asyncore.loop ()exceptKeyboardinterrupt:Print

1.3 Integration

Python Basic Tutorial Summary 15--5 Virtual Tea Party

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.