使用python的twisted框架编写一个简单的聊天室
2017-05-23 16:11
701 查看
下面是基本架构
# -*- coding:utf-8 -*-
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
user = {}
class ChatReci(LineReceiver): #定义一个类,这里继承的是LineReceiver
def __init__(self): #初始化2个变量
self.name = ''
self.state = "game"
def connectionMade(self): #连接协议,当客户端连接即发出消息
self.sendLine("input you name?")
def lineReceived(self, data): #这个函数定义了取名 打印欢迎界面,以及发送信息给连上来的用户
if self.name == '': #判断名字是否为空 如果为空就进行下面的操作
self.name = data #给self.name赋值
self.sendLine("you welcome, %s!" % (self.name)) #打印欢迎信息
user[self.name] = self #赋值给user
print '%s loging' %data #打印登录信息
else: #不为空就打印信息
message = "<%s> %s" % (self.name, data) #定义聊天信息
for ur,protocol in user.items(): #取他的用户名
if protocol != user: #判断他是不是一个用户 如果不是就传送消息
protocol.sendLine(message) #传送消息
factory = Factory() #定义工厂
factory.protocol = ChatReci #绑定我的类
reactor.listenTCP(22222, factory) #绑定端口和工厂
reactor.run() #启动
效果图:
# -*- coding:utf-8 -*-
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
user = {}
class ChatReci(LineReceiver): #定义一个类,这里继承的是LineReceiver
def __init__(self): #初始化2个变量
self.name = ''
self.state = "game"
def connectionMade(self): #连接协议,当客户端连接即发出消息
self.sendLine("input you name?")
def lineReceived(self, data): #这个函数定义了取名 打印欢迎界面,以及发送信息给连上来的用户
if self.name == '': #判断名字是否为空 如果为空就进行下面的操作
self.name = data #给self.name赋值
self.sendLine("you welcome, %s!" % (self.name)) #打印欢迎信息
user[self.name] = self #赋值给user
print '%s loging' %data #打印登录信息
else: #不为空就打印信息
message = "<%s> %s" % (self.name, data) #定义聊天信息
for ur,protocol in user.items(): #取他的用户名
if protocol != user: #判断他是不是一个用户 如果不是就传送消息
protocol.sendLine(message) #传送消息
factory = Factory() #定义工厂
factory.protocol = ChatReci #绑定我的类
reactor.listenTCP(22222, factory) #绑定端口和工厂
reactor.run() #启动
效果图:
相关文章推荐
- 使用python的twisted框架编写一个简单的聊天室
- 使用Python的Twisted框架编写简单的网络客户端
- python基于twisted框架编写简单聊天室
- 使用Python的Twisted框架实现一个简单的服务器
- 如何使用Python为Hadoop编写一个简单的MapReduce程序
- 使用Python来编写一个简单的感知机
- 一个使用面向对象方式编写的简单游戏框架
- 使用Python来编写一个简单的感知机
- 如何使用Python为Hadoop编写一个简单的MapReduce程序
- 如何使用Python为Hadoop编写一个简单的MapReduce程序(这个人T字还有好几篇精华的可以看)
- 使用Python的Twisted框架编写非阻塞程序的代码示例
- Python学习 - 编写一个简单的web框架(二)
- 使用Python的Twisted框架编写非阻塞程序的代码示例
- 使用过滤器+反射+枚举,简单模拟sturts2运行方式,编写一个属于自己的框架
- Python学习 - 编写一个简单的web框架(一)
- 如何使用Python为Hadoop编写一个简单的MapReduce程序
- 如何使用Python为Hadoop编写一个简单的MapReduce程序
- 使用Python编写一个简单的tic-tac-toe游戏的教程
- 使用Python的Scrapy框架编写web爬虫的简单示例
- 使用Python的Bottle框架写一个简单的服务接口的示例