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

Xu Ryan xuryans at gmail.com
Fri Dec 2 05:02:09 EST 2005


Thank you for you patient:)

The codes below seems have other problems:
1. one port bind one Proxy. And the proxy is also fixed before reactor run.
2. Should use other method make connect with localhost.
Perhaps it make my problem complicated.

I think i should make a schedule and check the command list, if it's
not empty, send the commands in the list. twisted has some module like
that, i rememberd.

> An example of using this module would be
>
>     from twisted.internet import reactor
>     from twisted.protocols.portforward import ProxyFactory
>     reactor.listenTCP(1234, ProxyFactory('somewhere.com', 5678))
>     reactor.run()
>
> If you run that it will start listening on port 1234, and do nothing else until
> you connect to that port.  As soon as a connection is established, a ProxyServer
> protocol is made, and its connectionMade handler will call reactor.connectTCP to
> establish a connection to somewhere.com's port 5678.
>
> Here's another example of making new connections in response to events that
> occur while the program is running.  You may find it simpler to understand,
> although I haven't tested it so there may be bugs, and it has essentially no
> error handling...:
>
> ----
> from twisted.protocols import basic
> from twisted.internet import reactor, protocol
>
> class MessageReceiver(basic.LineReceiver):
>     def lineReceived(self, line):
>         # expects lines like "host.somewhere.com 1234 here's a message".
>         host, port, message = line.split(' ', 2)
>         sendMessage(host, port, message)
>
> class MessageSender(protocol.Protocol):
>     def __init__(self, message):
>         self.message = message
>
>     def connectionMade(self):
>         # send the message
>         self.transport.write(message)
>         # close the connection
>         self.transport.loseConnection()
>
> def sendMessage(host, port, message):
>     cc = protocol.ClientCreator(reactor, MessageSender, message)
>     cc.connectTCP(host, port)
>
> f = protocol.ServerFactory()
> f.protocol = MessageReceiver
> reactor.listenTCP(1234, f)
> reactor.run()
> ----
>
> You may also want to work through the finger tutorial -- it's a bit long, but it
> does cover this sort of thing.
>
> -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