On 6/20/07, <b class="gmail_sendername">Delgado, Edgardo  CIV NAVAIR <a href="http://4.1.4.3">4.1.4.3</a></b> &lt;<a href="mailto:edgardo.delgado@navy.mil">edgardo.delgado@navy.mil</a>&gt; wrote:<div><span class="gmail_quote">
</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Here&#39;s the code:<br><br>from twisted.internet.protocol import ServerFactory, ClientFactory,
<br>Protocol, ClientCreator<br>from twisted.protocols.basic import LineReceiver<br>from twisted.application.service import Service, Application,<br>IServiceCollection<br>from twisted.application.internet import TCPServer, TCPClient
<br>from twisted.internet.stdio import StandardIO<br>from twisted.internet import reactor<br>import re<br><br>class DnLinkProtocol(LineReceiver):<br><br>&nbsp;&nbsp;&nbsp;&nbsp;def connectionMade(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sendLine(&quot;Connected from: &quot; + &quot;host &quot; +
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str(self.transport.getPeer().host) + &quot; &quot; +<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;port &quot; +<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str(self.transport.getPeer().port) + &#39;\n&#39;)<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;def lineReceived(self, line):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sendLine(&quot;DnLink echo: &quot; + line + &#39;\n&#39;)<br><br>class DSPEProtocol(LineReceiver):<br><br>&nbsp;&nbsp;&nbsp;&nbsp;def connectionMade(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sendLine(&quot;Connected from: &quot; + &quot;host &quot; +
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str(self.transport.getPeer().host) + &quot; &quot; +<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;port &quot; +<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str(self.transport.getPeer().port) + &#39;\n&#39;)<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;def lineReceived(self, line):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sendLine(&quot;DSPE echo: &quot; + line + &#39;\n&#39;)<br><br>class PAPSExecService(Service):<br><br>&nbsp;&nbsp;&nbsp;&nbsp;def getDnLinkInFactory(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f = ServerFactory()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.protocol = DnLinkProtocol<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.startService = self.startService<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return f<br><br>&nbsp;&nbsp;&nbsp;&nbsp;def getDSPEInFactory(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f = ServerFactory()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.protocol = DSPEProtocol<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.startService
 = self.startService<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return f<br><br>application = Application(&#39;PAPSExec&#39;)<br>f = PAPSExecService()<br>serviceCollection = IServiceCollection(application)<br>TCPServer(9595,<br>f.getDnLinkInFactory()).setServiceParent(serviceCollection)
<br>TCPServer(8585,<br>f.getDSPEInFactory()).setServiceParent(serviceCollection)<br><br><br>I would like that, for example, when data is received in port 9595 it<br>will forward the data to another server in port xxxx.<br>
Where would I create a TPC Client connection?<br>Where would I send the message?<br><br>Edgar<br><br>-----Original Message-----<br>From: <a href="mailto:twisted-python-bounces@twistedmatrix.com">twisted-python-bounces@twistedmatrix.com
</a><br>[mailto:<a href="mailto:twisted-python-bounces@twistedmatrix.com">twisted-python-bounces@twistedmatrix.com</a>] On Behalf Of Christian<br>Simms<br>Sent: Wednesday, June 20, 2007 11:01 AM<br>To: Twisted general discussion
<br>Subject: Re: [Twisted-Python] Server/Client<br><br>On 6/20/07, Delgado, Edgardo CIV NAVAIR <a href="http://4.1.4.3">4.1.4.3</a><br>&lt;<a href="mailto:edgardo.delgado@navy.mil">edgardo.delgado@navy.mil</a>&gt; wrote:<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I would like to build a server that when it gets a message on a<br>specific<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port it will &quot;forward&quot; that message to another port.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I.e. &quot;Server1&quot; listens on port 10000. When it gets data on port
<br>10000 it<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;will forward the data to &quot;Server2&quot; listening on port 15000.<br><br><br>Assuming you&#39;re using TCP, you could just create a server listening on a<br>port using reactor.listenTCP(port, factory) as detailed here
<br>&lt;<a href="http://twistedmatrix.com/projects/core/documentation/howto/servers.html">http://twistedmatrix.com/projects/core/documentation/howto/servers.html</a><br>&gt;&nbsp;&nbsp;and when you receive a message you would create a client using
<br>reactor.connectTCP(host, port, factory) as detailed here<br>&lt;<a href="http://twistedmatrix.com/projects/core/documentation/howto/clients.html">http://twistedmatrix.com/projects/core/documentation/howto/clients.html
</a><br>&gt;&nbsp;&nbsp;to then send the message.<br><br>Cheers,<br>Christian</blockquote><div><br>Assuming you want to create a new connection every time a client connects to your port 9595, it&#39;s slightly tricky because you  can&#39;t just forward the data as soon as you get it, since the connection to the other server may not be created yet.&nbsp; A relatively complex way is explained in the Python Cookbook in 
<a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/502293">article &quot;Hex-dump port-forwarding network proxy server&quot;</a>, where it queues up the data until the outgoing connection is made.&nbsp; A simpler way is used in 
twisted.protocols.portforward.ProxyFactory/ProxyServer, where it just pauses receiving data until the outgoing connection is made.&nbsp; I found a <a href="http://mail.python.org/pipermail/python-list/2003-June/212102.html">Python mailing list entry
</a> which explains how to override the portforward&#39;s methods.<br><br>Cheers,<br>Christian<br><br></div><br></div>