设为首页 加入收藏

TOP

3..Twisted学习(一)
2018-12-05 18:09:57 】 浏览:173
Tags:3..Twisted 学习

写这个主要是为了自己理解Twisted的文档

建立一个finger服务

你不需要调用Twisted,Twisted会自己运行。reactor是Twisted的主循环,想python的其他主循环一样。每个Twisted只有一个reactor。一旦启动他就会不停的运行下去,响应一个又一个请求。

from twisted.internet import reactor返回当前的reactor。如果你不选择一个reactor的话,它会默认选择。

 1 from twisted.internet import protocol, reactor, endpoints
 2 
 3 class FingerProtocol(protocol.Protocol):
 4     pass
 5 
 6 class FingerFactory(protocol.ServerFactory):
 7     protocol = FingerProtocol
 8 
 9 fingerEndpoint = endpoints.serverFromString(reactor, "tcp:1079")
10 fingerEndpoint.listen(FingerFactory())
11 reactor.run()

这个流程看过前两章的应该很熟悉了,先创建协议,再创建工厂,然后主循环。

 1 from twisted.internet import protocol, reactor, endpoints
 2 
 3 class FingerProtocol(protocol.Protocol):
 4     def connectionMade(self):
 5         self.transport.loseConnection()
 6 
 7 class FingerFactory(protocol.ServerFactory):
 8     protocol = FingerProtocol
 9 
10 fingerEndpoint = endpoints.serverFromString(reactor, "tcp:1079")
11 fingerEndpoint.listen(FingerFactory())
12 reactor.run()

这个是增加了一个连接之后的初始化配置

 1 from twisted.internet import protocol, reactor, endpoints
 2 from twisted.protocols import basic
 3 
 4 class FingerProtocol(basic.LineReceiver):
 5     def lineReceived(self, user):
 6         self.transport.loseConnection()
 7 
 8 class FingerFactory(protocol.ServerFactory):
 9     protocol = FingerProtocol
10 
11 fingerEndpoint = endpoints.serverFromString(reactor, "tcp:1079")
12 fingerEndpoint.listen(FingerFactory())
13 reactor.run()

这个是按行读取数据,就是按照你的\r\n读取数据,之后关闭连接

写一个读取用户名,返回错误,断开连接的finger

 1 from twisted.internet import protocol, reactor, endpoints
 2 from twisted.protocols import basic
 3 
 4 class FingerProtocol(basic.LineReceiver):
 5     def lineReceived(self, user):
 6         self.transport.write(b"No such user\r\n")
 7         self.transport.loseConnection()
 8 
 9 class FingerFactory(protocol.ServerFactory):
10     protocol = FingerProtocol
11 
12 fingerEndpoint = endpoints.serverFromString(reactor, "tcp:1079")
13 fingerEndpoint.listen(FingerFactory())
14 reactor.run()

这个就是按行读取,返回一句话,之后关闭连接

 1 from twisted.internet import protocol, reactor, endpoints
 2 from twisted.protocols import basic
 3 
 4 class FingerProtocol(basic.LineReceiver):
 5     def lineReceived(self, user):
 6         self.transport.write(self.factory.getUser(user)+ b"\r\n")
 7         self.transport.loseConnection()
 8 
 9 class FingerFactory(protocol.ServerFactory):
10     protocol = FingerProtocol
11 
12     def getUser(self, user):
13         return b"No such user"
14 
15 fingerEndpoint = endpoints.serverFromString(reactor, "tcp:1079")
16 fingerEndpoint.listen(FingerFactory())
17 reactor.run()

增加获取用户名的函数

在协议之中调用工厂方法,可以用self.factory,这个就是指向工厂的,因为工厂类实际上是它的父类。

 1 from twisted.internet import protocol, reactor, endpoints
 2 from twisted.protocols import basic
 3 
 4 class FingerProtocol(basic.LineReceiver):
 5     def lineReceived(self, user):
 6         self.transport.write(self.factory.getUser(user) + b"\r\n")
 7         self.transport.loseConnection()
 8 
 9 class FingerFactory(protocol.ServerFactory):
10     protocol = FingerProtocol
11 
12     def __init__(self, users):
13         self.users = users
14 
15     def getUser(self, user):
16         return self.users.get(user, b"No such user")
17 
18 fingerEndpoint = endpoints.serverFromString(reactor, &q
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇python简单实现学生管理系统 下一篇day 08 文件操作

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目