Have you ever thought about developing a graphical interface for a chat room program with the python you've learned?
Like this:
If you want to develop such a kind of nostalgic chat program, then you can continue to look;
To develop this chat program, you need to have the following knowledge points:
- Asyncore, Asynchat module use
- WxPython Graphics Development
The principle of this chat program is as follows:
Because Python is a language with GIL, it is not a good choice to use multithreading in Python for too many tasks. At the same time the chat server will communicate with multiple sockets, so we can implement the chat server based on the Asyncore module.
The Aysncore module is an asynchronous socket processor that makes it much easier to do asynchronous programming with this module. The Asynchat module is further encapsulated on the basis of the Asyncore module, simplifying the difficulty of developing the communication task based on the text protocol.
Now that you're developing a chat program, it's necessary to design the protocol used for chatting. For the sake of simplicity, the chat server we are going to develop supports only text protocols, and calls related operations by command message. For example, if the client sends the following text, the appropriate action will be taken
# 登录操作login\n# 在聊天室中发表 hello 内容say hello\n# 查看聊天室在线用户look\n# 退出登录logout\n
In the above protocol flow, login, say, look, logout is the relevant protocol code.
The development steps are as follows:
- 1, server class: A chat server class, through the inheritance of Asyncore dispatcher class to achieve;
- 2, session class: with the server class also need to be able to maintain each user's connection session, here inherit Asynchat
async_chat
class to implement;
- 3, Protocol command interpreter: Chat Server protocol, specifically, processing user login, exit, send messages, query online user code;
- 4, Room: realize the room of the chat room, here we define three kinds of rooms, respectively is the user just log in the room, chat room and log out of the room;
- 5, Landing window: The implementation of the client;
- 6, Chat window: The most important thing in the Chat window is to send a message to the server and accept the server message;
Well, what? Do you see this principle and development step or do not develop specifically, and do not write code? Then I give you a detailed development of the tutorial, see?!
"Python implements text chat room"
This tutorial detailed introduction of the development steps and development code, take you one step at the realization of the text chat room, and there is an online development environment, you can follow the hands to operate OH;
Python implementation text chat room