[Twisted-Python] Server/Client

I would like to build a server that when it gets a message on a specific port it will "forward" that message to another port. I.e. "Server1" listens on port 10000. When it gets data on port 10000 it will forward the data to "Server2" listening on port 15000. Edgar

On 6/20/07, Delgado, Edgardo CIV NAVAIR 4.1.4.3 <edgardo.delgado@navy.mil> wrote:
I would like to build a server that when it gets a message on a specific port it will "forward" that message to another port. I.e. "Server1" listens on port 10000. When it gets data on port 10000 it will forward the data to "Server2" listening on port 15000.
Assuming you're using TCP, you could just create a server listening on a port using reactor.listenTCP(port, factory) as detailed here<http://twistedmatrix.com/projects/core/documentation/howto/servers.html>and when you receive a message you would create a client using reactor.connectTCP(host, port, factory) as detailed here<http://twistedmatrix.com/projects/core/documentation/howto/clients.html>to then send the message. Cheers, Christian

Here's the code: from twisted.internet.protocol import ServerFactory, ClientFactory, Protocol, ClientCreator from twisted.protocols.basic import LineReceiver from twisted.application.service import Service, Application, IServiceCollection from twisted.application.internet import TCPServer, TCPClient from twisted.internet.stdio import StandardIO from twisted.internet import reactor import re class DnLinkProtocol(LineReceiver): def connectionMade(self): self.sendLine("Connected from: " + "host " + str(self.transport.getPeer().host) + " " + "port " + str(self.transport.getPeer().port) + '\n') def lineReceived(self, line): self.sendLine("DnLink echo: " + line + '\n') class DSPEProtocol(LineReceiver): def connectionMade(self): self.sendLine("Connected from: " + "host " + str(self.transport.getPeer().host) + " " + "port " + str(self.transport.getPeer().port) + '\n') def lineReceived(self, line): self.sendLine("DSPE echo: " + line + '\n') class PAPSExecService(Service): def getDnLinkInFactory(self): f = ServerFactory() f.protocol = DnLinkProtocol f.startService = self.startService return f def getDSPEInFactory(self): f = ServerFactory() f.protocol = DSPEProtocol f.startService = self.startService return f application = Application('PAPSExec') f = PAPSExecService() serviceCollection = IServiceCollection(application) TCPServer(9595, f.getDnLinkInFactory()).setServiceParent(serviceCollection) TCPServer(8585, f.getDSPEInFactory()).setServiceParent(serviceCollection) I would like that, for example, when data is received in port 9595 it will forward the data to another server in port xxxx. Where would I create a TPC Client connection? Where would I send the message? Edgar -----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com] On Behalf Of Christian Simms Sent: Wednesday, June 20, 2007 11:01 AM To: Twisted general discussion Subject: Re: [Twisted-Python] Server/Client On 6/20/07, Delgado, Edgardo CIV NAVAIR 4.1.4.3 <edgardo.delgado@navy.mil> wrote: I would like to build a server that when it gets a message on a specific port it will "forward" that message to another port. I.e. "Server1" listens on port 10000. When it gets data on port 10000 it will forward the data to "Server2" listening on port 15000. Assuming you're using TCP, you could just create a server listening on a port using reactor.listenTCP(port, factory) as detailed here <http://twistedmatrix.com/projects/core/documentation/howto/servers.html
and when you receive a message you would create a client using reactor.connectTCP(host, port, factory) as detailed here <http://twistedmatrix.com/projects/core/documentation/howto/clients.html to then send the message.
Cheers, Christian

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

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

On 6/20/07, Delgado, Edgardo CIV NAVAIR 4.1.4.3 <edgardo.delgado@navy.mil> wrote:
I would like to build a server that when it gets a message on a specific port it will "forward" that message to another port. I.e. "Server1" listens on port 10000. When it gets data on port 10000 it will forward the data to "Server2" listening on port 15000.
This is not Twisted, but it may be helpful: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/483732 Arnar

It's neither Twisted or Python, but if you're on Linux the socat tool does exactly what you're asking and a lot more. Google for more info I use it all the time, and under heavy workloads it's never failed on me yet. Regards Dave M. On 21/06/07, Delgado, Edgardo CIV NAVAIR 4.1.4.3 <edgardo.delgado@navy.mil> wrote:
I would like to build a server that when it gets a message on a specific port it will "forward" that message to another port. I.e. "Server1" listens on port 10000. When it gets data on port 10000 it will forward the data to "Server2" listening on port 15000.
Edgar
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

OK, I got the portforwarding stuff done using protocols.portforward. But this is good for 1:1 relationship, i.e. listen on one port and forwarding to another. What I need is a 1:many relationship, i.e. listen on one port and forwarding to many ports. Edgar -----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com] On Behalf Of Delgado, Edgardo CIV NAVAIR 4.1.4.3 Sent: Wednesday, June 20, 2007 10:16 AM To: twisted-python@twistedmatrix.com Subject: [Twisted-Python] Server/Client I would like to build a server that when it gets a message on a specific port it will "forward" that message to another port. I.e. "Server1" listens on port 10000. When it gets data on port 10000 it will forward the data to "Server2" listening on port 15000. Edgar _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

Suggestion: If data reliability is not an issue, it's pretty much easier to do it using UDP (twisted.internet.protocol.DatagramProtocol). You won't need to deal with connections. "What I need is a 1:many relationship, i.e. listen on one port and forwarding to many ports." A UDP MulticastServer is what you need. ^_^ On 6/28/07, Delgado, Edgardo CIV NAVAIR 4.1.4.3 <edgardo.delgado@navy.mil> wrote:
OK, I got the portforwarding stuff done using protocols.portforward. But this is good for 1:1 relationship, i.e. listen on one port and forwarding to another. What I need is a 1:many relationship, i.e. listen on one port and forwarding to many ports.
Edgar
-----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com] On Behalf Of Delgado, Edgardo CIV NAVAIR 4.1.4.3 Sent: Wednesday, June 20, 2007 10:16 AM To: twisted-python@twistedmatrix.com Subject: [Twisted-Python] Server/Client
I would like to build a server that when it gets a message on a specific port it will "forward" that message to another port. I.e. "Server1" listens on port 10000. When it gets data on port 10000 it will forward the data to "Server2" listening on port 15000.
Edgar
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

I already took a look at UDP, but we need data reliability so can't use UDP. :-( Thx, Edgar -----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com] On Behalf Of Alvin Delagon Sent: Wednesday, June 27, 2007 9:55 PM To: Twisted general discussion Subject: Re: [Twisted-Python] Server/Client Suggestion: If data reliability is not an issue, it's pretty much easier to do it using UDP (twisted.internet.protocol.DatagramProtocol). You won't need to deal with connections. "What I need is a 1:many relationship, i.e. listen on one port and forwarding to many ports." A UDP MulticastServer is what you need. ^_^ On 6/28/07, Delgado, Edgardo CIV NAVAIR 4.1.4.3 <edgardo.delgado@navy.mil> wrote: OK, I got the portforwarding stuff done using protocols.portforward. But this is good for 1:1 relationship, i.e. listen on one port and forwarding to another. What I need is a 1:many relationship, i.e. listen on one port and forwarding to many ports. Edgar -----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto: twisted-python-bounces@twistedmatrix.com <mailto:twisted-python-bounces@twistedmatrix.com> ] On Behalf Of Delgado, Edgardo CIV NAVAIR 4.1.4.3 Sent: Wednesday, June 20, 2007 10:16 AM To: twisted-python@twistedmatrix.com <mailto:twisted-python@twistedmatrix.com> Subject: [Twisted-Python] Server/Client I would like to build a server that when it gets a message on a specific port it will "forward" that message to another port. I.e. "Server1" listens on port 10000. When it gets data on port 10000 it will forward the data to "Server2" listening on port 15000. Edgar _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python <http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python>
participants (5)
-
Alvin Delagon
-
Arnar Birgisson
-
Christian Simms
-
David Mitchell
-
Delgado, Edgardo CIV NAVAIR 4.1.4.3