[Twisted-Python] How can a tcp client connect with multi servers?

Xu Ryan xuryans at gmail.com
Thu Dec 1 22:42:49 EST 2005


Would you like show me an simple example about this? I wrote one, but
it dosn't work.

__sender = None

class p(Protocol):
    def connectionMade(self):
        print "Connecttion make"

    def sendMessage(self):
        self.transport.write("some message")

    def connectionLost(self, reason):
        print "Lost, reason", reason

def sendCmd(addr, port, cmd, task, options = ""):
    """Send Cmd to
    """
    global __sender

    __sender.connectTCP(addr, port).addCallback(send,\
            cmd, task, options)

def send(p,cmd, task, options):
    print "send"
    p.sendMessage()
    return p

def finishConnection(p):
    print "lostConnection"
    p.transport.loseConnection()

def initSendCommand():
    global __sender
    if not __sender:
        __sender = ClientCreator(reactor, p)

if __name__ == "__main__":
    def testSendMessage():
        time.sleep(2)
        sendCmd("localhost", 8009, "test send")

    initSendCommand()
    thread.start_new_thread(testSendMessage, ())
    reactor.run()


On 12/1/05, Andrew Bennetts <andrew-twisted at puzzling.org> wrote:

> You seem to misunderstand how to do concurrent operations in Twisted.  You don't
> need to stop the reactor.  If at some point in your code you want to make
> another connection with ClientCreator, just call ClientCreator's connectTCP
> method as usual -- without stopping the reactor.  The point of the reactor is
> that it takes care of managing multiple connections and the like at once.
>
> So, it's not "stop [the reactor] , re-create ClientCreator, then call
> reactor.run() to send the message", it's just "re-create ClientCreator".  It may
> even be just "call connectTCP on your existing ClientCreator again."
>
> -Andrew.
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>


--
Xu Ryans




More information about the Twisted-Python mailing list