listing24-4.py

来自「《Beginning Python--From Novice to Profes」· Python 代码 · 共 41 行

PY
41
字号
from asyncore import dispatcherfrom asynchat import async_chatimport socket, asyncorePORT = 5005class ChatSession(async_chat):    def __init__(self, sock):        async_chat.__init__(self, sock)        self.set_terminator("\r\n")        self.data = []    def collect_incoming_data(self, data):        self.data.append(data)    def found_terminator(self):        line = ''.join(self.data)        self.data = []        # Do something with the line...        print lineclass ChatServer(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)        self.sessions = []    def handle_accept(self):        conn, addr = self.accept()        self.sessions.append(ChatSession(conn))if __name__ == '__main__':    s = ChatServer(PORT)    try: asyncore.loop()    except KeyboardInterrupt: print

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?