python手记-twisted(5)

本文介绍使用 Python 的 Twisted 框架实现一个简易的聊天室应用。该聊天室支持用户输入昵称并进行群聊或私聊。文章展示了如何通过 Twisted 的 Factory 和 Protocol 类来搭建服务器及处理客户端连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

聊天室

# -*- coding: utf-8 -*-
#DeepChat 0.1
#author:myhaspl@myhaspl.com
#license:LGPL

from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor

class Chat(LineReceiver):
    def __init__(self,users,myfactory):
        self.users=users;
        self.name=None
        self.state="NAME"
        self.factory=myfactory
    def strutf8(selfr,showstr):
        retstr=showstr.encode('utf-8')
        return retstr
    def connectionMade(self):
        self.factory.connectedcount=self.factory.connectedcount+1
        self.sendLine(self.strutf8(U"【deepchat】********本聊天室共 %d 人聊天********"%self.factory.connectedcount))
        self.sendLine(self.strutf8(U"请输入您的妮称:"))

    def connectionLost(self,reason):
        self.factory.connectedcount=self.factory.connectedcount-1
        self.sendLine(self.strutf8(U"%s 再见"%self.name))
        if self.name in self.users:
            del self.users[self.name]
            

    def getName(self,myline):
        if myline in self.users:
            self.sendLine(self.strutf8(U"%s 已经登录,请不要重复登录!"% myline))
            return
        
        self.name=myline
        self.users[self.name]=self
        self.state="CHAT"
        self.sendLine(self.strutf8(U"欢迎您,%s"%self.name))          

    def getChat(self,myline):
        mychat=myline.split("#")
        lcount=len(mychat)
        if lcount>1:                    
            if mychat[0] in self.users:
                self.users[mychat[0]].sendLine(self.strutf8(U"<%s> to <%s> : %s"%(self.name,mychat[0],mychat[1].decode('utf-8'))))
            else:
                self.sendLine(self.strutf8(U"warning: %s 没有登录聊天室!"%mychat[0])) 
        else:
            for name,protocol in self.users.iteritems():
                if protocol!=self:
                    protocol.sendLine(self.strutf8(U"<%s>: %s"%(self.name,myline.decode('utf-8'))))
           
  
    def lineReceived(self,line):
        myline=line.strip()
        if myline=="":
            if self.state=="NAME":
                self.sendLine(self.strutf8(U"本聊天室不允许游客进入"))
                self.transport.loseConnection()
            return
        if self.state=="NAME":
            self.getName(myline)
        else:
            self.getChat(myline)

class chatFactory(Factory):
    connectedcount=0
    protocol=Chat
    def __init__(self):
        self.users={}
        self.myfactory=self
    def buildProtocol(self,addr):
        return Chat(self.users,self.myfactory)

reactor.listenTCP(8001,chatFactory())
reactor.run()

[c:\~]$ telnet 120.55.*.* 8001

本博客所有内容是原创,如果转载请注明来源

http://blog.youkuaiyun.com/myhaspl/




Connecting to 120.55.*.*:8001...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
【deepchat】********本聊天室共 3 人聊天********
请输入您的妮称:
x1
x1 已经登录,请不要重复登录!
x2
欢迎您,x2
xx2# asdfasfasdfs


[c:\~]$ telnet 120.55.*.* 8001




Connecting to 120.55.*.*:8001...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
【deepchat】********本聊天室共 2 人聊天********
请输入您的妮称:
xx2
欢迎您,xx2
磊柘城苛
<x3>: 柘城村要在地愿为连理枝地 夺厅要权志龙
<x2> to <xx2> :  asdfasfasdfs


[c:\~]$ telnet 120.55.*.* 8001




Connecting to 120.55.*.*:8001...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
【deepchat】********本聊天室共 1 人聊天********
请输入您的妮称:
x1
欢迎您,x1
您好,大家好
<xx2>: 磊柘城苛
<x3>: 柘城村要在地愿为连理枝地 夺厅要权志龙


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值