当前位置:编程学习 > python >>

[Python-*-读书]Python基础教程--虚拟茶话会

Python教程  这里是第二十四章的源码,练习研究之用


[python] 
#coding:utf8   
#python2.7 chatSer2.py  
#稍微复杂一点的聊天室服务器  
'''''
用python启动服务 telnet连接之后就可以变成简单的聊天服务器
第一次登陆:login name 
登出: logout
说话: say 
查看谁登陆过服务器:who 
查看同一个房间的人 : look 
 
简单例子:
telnet localhost 50005
Welcome to TestChat
login tianshi
diyu has entered the room.
nih
Unknown command: ih
say nihao
tianshi: nihao
diyu: niyehao
 
''' 
 
from asyncore import dispatcher  
from asynchat import async_chat 
import socket 
import asyncore 
 
HOST = '' 
PORT = 50005 
SERVERCONF = (HOST,PORT) 
NAME = 'TestChat' 
 
class EndSession(Exception): 
    pass  
 
class CommandHandler(object): 
    '''''
    命令解析类
    ''' 
     
    def unknown(self,session,cmd): 
        '响应未知的命令' 
        session.push('Unknown command:%s \r\n' %cmd) 
 
    def handle(self,session,line): 
        '处理从客户端发来的信息 解析命令和内容' 
        if not line: 
            print 'please write a command...' 
             
        if not line.strip():  
            print 'get a line :',line  
            return 
         
        parts = line.split(' ',1) 
        #命令分离  
        cmd = parts[0] 
        try: 
            line = parts[1].strip() 
        except IndexError: 
            line = '' 
        #调用相应的方法处理命令  
        meth = getattr(self, 'do_'+cmd, None) 
        try: 
            #调用命令对应的方法  
            meth(session,line) 
        except TypeError: 
            self.unknown(session, cmd) 
             
class Room(CommandHandler): 
    '''''
    包括一个或多个用户(会话)的泛型环境。负责处理基本的命令和广播
    ''' 
     
    def __init__(self,server): 
        self.server = server 
        self.sessions = [] 
         
    def add(self, session): 
        'add a user' 
        self.sessions.append(session) 
         
    def remove(self,session): 
        'a user leave' 
        self.sessions.remove(session) 
         
    def broadcast(self,line): 
        'give all session in the room a line' 
        for session in self.sessions: 
            session.push(line) 
         
    def do_logout(self,session,line): 
        '响应登出命令' 
        raise EndSession 
     
class LoginRoom(Room): 
    '''''
    为刚刚登陆的用户准备的房间
    ''' 
     
    def add(self,session): 
        Room.add(self, session) 
        self.broadcast('Welcome to %s \r\n' %self.server.name) 
         
    def unknown(self, session, cmd): 
        session.push('Please login \nUse "login <nick>"\r\n') 
         
    def do_login(self, session, line): 
        name = line.strip() 
        #登陆时必须指定一个用户名  
        if not name: 
            session.push('Please enter a name\r\n') 
        elif name in self.server.users: #用户名不可重复 ?  
            session.push('The name "%s" is taken! \r\n' %name) 
            session.push('Please try again.\r\n') 
        else: 
            session.name = name 
            session.enter(self.server.main_room) 
             
class ChatRoom(Room): 
    '''''
    为多用户在一起聊天准备的房间
    ''' 
    def add(self, session): 
        #tell all new one come  
        self.broadcast(session.name +' has entered the room. \r\n') 
        self.server.users[session.name] = session 
        Room.add(self,

补充:Web开发 , Python ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,