小编use*_*302的帖子

Twisted - 将数据从服务器发送到客户端

我是Twisted的新手,并且阅读了许多与我遇到的问题类似的相关帖子.但是,我无法推断以前的答案来解决我的简单问题.我确实参考了Twisted的FAQ部分 - 我仍然无法弄清楚.

我的问题是我有一个服务器在一个端口监听,当我收到一个"START"命令时,我想和几个客户交谈.作为一个例子,我使用了一个客户端,它为我提供了一个幸运饼干.但是,我无法在服务器代码中与客户端通信.你能告诉我哪里出错吗?这是代码:

from twisted.internet import reactor, protocol
from twisted.internet.protocol import Protocol, Factory

class FakeTelnet(Protocol):
    def connectionMade(self):
        print 'local connection made'
        self.otherFact = protocol.ClientFactory()
        self.otherFact.protocol = EchoClient
        self.factory.clients.append(self.otherFact.protocol)
        reactor.connectTCP('psrfb6',10999, self.otherFact)

    def dataReceived(self, data):

        if 'START' in data:
            # send a command to cookie server.
            for client in self.factory.clients:
                client.transport.write('START\r\n')

    def connectionLost(self):
        print 'connection lost'

class EchoClient(Protocol):
    """Once connected, send a message, then print the result."""

    def connectionMade(self):
        print "Connection to cookie server"

    def dataReceived(self, data):
        "As soon as any …
Run Code Online (Sandbox Code Playgroud)

python twisted

4
推荐指数
1
解决办法
3351
查看次数

标签 统计

python ×1

twisted ×1