[Twisted-Python] Server/Client
Delgado, Edgardo CIV NAVAIR 4.1.4.3
edgardo.delgado at navy.mil
Thu Jun 21 12:36:50 EDT 2007
Thanks Christian (and everybody else) for your help,
Edgar
-----Original Message-----
From: twisted-python-bounces at twistedmatrix.com
[mailto:twisted-python-bounces at 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 at 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 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
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
More information about the Twisted-Python
mailing list