学习笔记 提取常量和一个简单的聊天系统(python)from python基础教程

 

提取常量:将数据中的常量和配置常量保存的文件中,写程序时在文件中可以直接提取

.txt

[nember]

pi: 3.1415926

[message]

greeting: welcome 

questing: put in radius:

result: The area is

test.py

from ConfigParser import ConfigParserCONFIGFILE='文件存放地址'

config=ConfigParser()

config.read(CONFIGFILE)

print config.get('message','greeting')

radius=input(config.get('message','questing')+' ')

print config.get('message','result')

print config.getfloat('number','pi')*radius**2

可以用telnet访问的聊天系统

 
# -*- coding: utf-8 -*-
from asyncore import dispatcher
from asynchat import async_chat
import socket,asyncore

PORP=5005 
NAME='TestChat123'

class CommandHandler:
    def unknown(self,session,cmd):
        session.push('Unknown command: %s\r\n'% cmd)

    def handle(self,session,line):
        if not line.strip():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 TabError:
            self.unknown(session,cmd)

class Room(CommandHandler):
    def __init__(self,server):
        self.server=server
        self.sessions=[]

    def add(self,session):
        self.sessions.append(session)

    def remove(self,session):
        self.sessions.remove(session)

    def broadcast(self,line):
        for session in self.sessions:
            session.push(line)

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 log in\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 enter a name\r\n')
        else:
            session.name=name
            session.enter(self.server.main_room)

class ChatRoom(Room):
    def add(self,session):
        Room.add(self,session)
        self.broadcast(session.name+' has entered the room\r\n')
        self.server.users[session.name]=session

    def remove(self,session):
        Room.remove(self,session)
        self.broadcast(session.name+' has left the room\r\n')


    def do_say(self,session,line):
        self.broadcast(session.name+': '+line+'\r\n')


    def do_look(self,session,line):
        session.push('in this room: ')
        for other in  self.sessions:
            session.push(other.name+'\r\n')

    def do_who(self,session,line):
        session.push('in login')
        for name in  self.server.users:
            session.push(name+'\r\n')

class LogoutRoom(Room):
    def add(self,session):
        try:del self.server.users[session.name]
        except KeyError:pass

class Chatsession(async_chat):
    def __init__(self,server,sock):
        async_chat.__init__(self,sock)
        self.server=server
        self.set_terminator('\r\n')
        self.data=[]
        self.name=None
        self.enter(LoginRoom(server))

    def enter(self,room):
        try:cur=self.room
        except AttributeError:pass
        else:cur.remove(self)
        self.room=room
        room.add(self)

    def collect_incoming_data(self, data):
        self.data.append(data)

    def found_terminator(self):
        line=''.join(self.data)
        self.data=[]
        try:self.room.handle(self,line)
        except EnvironmentError:
            self.handle_close()

    def handle_close(self):
        async_chat.handle_close(self)
        self.enter(LogoutRoom(self.server))

class ChatServer(dispatcher):
    def __init__(self,port,name):
        dispatcher.__init__(self)
        self.create_socket(socket.AF_INET,socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind(('',port))
        self.listen(5)
        self.name=name
        self.users={}
        self.main_room=ChatRoom(self)

    def handle_accept(self):
        conn,addr=self.accept()
        Chatsession(self,conn)

if __name__=='__main__':
    s=ChatServer(PORP,NAME)
    asyncore.loop()
 
 
 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值