开发者

Twisted - send data from a server to client

开发者 https://www.devze.com 2023-03-22 02:28 出处:网络
I am new to Twisted, and have read many related posts similar to the problem that I have. However, I am unable to extrapolate the previous answers to solve my simple problem. I did refer to the FAQ se

I am new to Twisted, and have read many related posts similar to the problem that I have. However, I am unable to extrapolate the previous answers to solve my simple problem. I did refer to the FAQ section of Twisted - I still can't figure out.

My problem is that I have a server listening at one port, and when I receive a "START" command, I would like to talk to several clients. As an example, I used a single client, that serves me a fortune cookie. However, I am unable to talk to client from with in the server code. Can you please tell where I am going wrong? Here is the code:

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 c开发者_如何学JAVAonnectionMade(self):
        print "Connection to cookie server"

    def dataReceived(self, data):
        "As soon as any data is received, write it back."
        print "Fortune Server said:", data

    def connectionLost(self):
        print "connection lost"

    def send_stuff(data):
        self.transport.write(data);

class MyFactory(Factory):
    protocol = FakeTelnet
    def __init__(self, EchoClient):
        self.clients = []
        self.otherCli = EchoClient

reactor.listenTCP(5823, MyFactory(EchoClient))
reactor.run()


class FakeTelnet(Protocol):
    def connectionMade(self):
        print 'local connection made'
        self.otherFact = protocol.ClientFactory()
        self.otherFact.protocol = EchoClient

Here, you set self.otherFact.protocol to be the EchoClient class.

        self.factory.clients.append(self.otherFact.protocol)

Here you add the EchoClient class to the self.factory.clients list. This can only cause self.factory.clients to be a list full of the EchoClient class over and over again.

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')

Here you try to write to EchoClient.transport which will generally be None, since it is only ever instances of a protocol class which get connected to a transport, not the classes themselves.

Try adding connected instances to self.factory.clients instead. You are managing to create such instances, I think, since you also have this line in FakeTelnet.connectionMade:

    reactor.connectTCP('psrfb6',10999, self.otherFact)

However, you aren't doing anything that would let you get the connected protocol into the self.factory.clients list. Perhaps you could define self.otherFact like this instead:

class OtherFactory(ClientFactory):
    protocol = EchoClient

    def __init__(self, originalFactory):
        self.originalFactory = originalFactory

    def buildProtocol(self, addr):
        proto = ClientFactory.buildProtocol(self, addr)
        self.originalFactory.clients.append(proto)
        return proto

You will also want to remove things from the clients list at some point. Perhaps in the connectionLost callback of the protocol:

class EchoClient(Protocol):
    ...
    def connectionLost(self, reason):
        self.factory.originalFactory.clients.remove(self)

Finally, you may want to use twisted.protocols.basic.LineOnlyReceiver as the base class for FakeTelnet and put your logic in its lineReceived callback, if you are dealing with line-oriented data. The dataReceived callback does not make guarantees about data boundaries, so you may never see a single string containing "START". For example, you might get two calls to dataReceived, one with "ST" and the other with "ART".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号