[Twisted-Python] Server/Client

Delgado, Edgardo CIV NAVAIR 4.1.4.3 edgardo.delgado at navy.mil
Wed Jun 20 15:42:17 EDT 2007


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 at twistedmatrix.com
[mailto:twisted-python-bounces at 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 at 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





More information about the Twisted-Python mailing list