正确的使用方式Twisted Services如何?

时间:2014-12-12 14:44:28

标签: python service twisted

我开始学习twisted python

但是当我尝试使用官方示例进行一些实验时,我无法通过扭曲的服务获得成功

让我们看一下这个例子:

http://twistedmatrix.com/documents/14.0.0/_downloads/finger12.tac

class FingerProtocol(basic.LineReceiver):

    def lineReceived(self, user):

        print '+++++++++ line getter' # the execution of this app never print this line


class FingerSetterProtocol(basic.LineReceiver):

    def lineReceived(self, line):
        print '+++++++++ line setter' # the execution of this app never print this line
        self.lines.append(line)

来自扭曲的结果:

shell$ twistd -noy pizza1.tac 

2014-12-12 16:04:57+0200 [-] Log opened.

2014-12-12 16:04:57+0200 [-] twistd 13.2.0 (/usr/bin/python 2.7.6) starting up.

2014-12-12 16:04:57+0200 [-] reactor class: twisted.internet.epollreactor.EPollReactor.

2014-12-12 16:04:57+0200 [-] FingerFactory starting on 9000

2014-12-12 16:04:57+0200 [-] Starting factory <__builtin__.FingerFactory instance at 
0x7f3b66dfd998>

2014-12-12 16:04:57+0200 [-] FingerSetterFactory starting on 9001

2014-12-12 16:04:57+0200 [-] Starting factory <__builtin__.FingerSetterFactory instance at 0x7f3b66dfdbd8>

2014-12-12 16:04:57+0200 [-] set uid/gid 1000/1000

pip冻结的结果:

Twisted==14.0.2

argparse==1.2.1

wsgiref==0.1.2

zope.interface==4.1.1

python版本:

  

Python 2.7.6(默认,2014年3月22日,22:59:56)

如果有人知道lineReceived未运行的原因,将很乐意提供帮助

我的测试代码:

from twisted.application import internet, service
from twisted.internet import protocol, reactor, defer
from twisted.protocols import basic


class FingerProtocol(basic.LineReceiver):

    def connectionMade(self):
        print '+++++++++ getter is coming'

    def lineReceived(self, user):

        print '+++++++++ line getter'

        d = self.factory.getUser(user)

        def onError(err):

            return '+++++++++ Internal error in server'

        d.addErrback(onError)

        def writeResponse(message):

            print '+++++++++ getter write'
            self.transport.write(message + '\r\n')
            self.transport.loseConnection()

        d.addCallback(writeResponse)


class FingerFactory(protocol.ServerFactory):

    protocol = FingerProtocol

    def __init__(self, **kwargs):
        print '+++++++++ init factory'
        self.users = kwargs

    def getUser(self, user):
        print '+++++++++ get user'
        return defer.succeed(self.users.get(user, "No such user"))

class FingerSetterProtocol(basic.LineReceiver):

    def connectionMade(self):
        print '+++++++++ setter is coming'
        self.lines = []

    def lineReceived(self, line):
        print '+++++++++ line setter'
        self.lines.append(line)

    def connectionLost(self, reason):
        print '+++++++++ connection lost'
        user = self.lines[0]
        status = self.lines[1]
        self.factory.setUser(user, status)


class FingerSetterFactory(protocol.ServerFactory):

    protocol = FingerSetterProtocol

    def __init__(self, fingerFactory):
        print '+++++++++ init setter factory'
        self.fingerFactory = fingerFactory

    def setUser(self, user, status):
        print '+++++++++ set user'
        self.fingerFactory.users[user] = status

ff = FingerFactory(targy='Happy and well')
fsf = FingerSetterFactory(ff)

application = service.Application('finger', uid=1000, gid=1000)
serviceCollection = service.IServiceCollection(application)
internet.TCPServer(9000,ff).setServiceParent(serviceCollection)
internet.TCPServer(9001,fsf).setServiceParent(serviceCollection)

0 个答案:

没有答案