I have been using following twisted python script to listen for the requests on port 8080 and forward the request to specific IP.
Here is the script:
class ServerProtocol(protocol.Protocol):
def __init__(self):
self.buffer = None
self.client = None
def connectionMade(self):
factory = protocol.ClientFactory()
factory.protocol = ClientProtocol
factory.server = self
reactor.connectTCP('x.x.x.x', 80, factory)
def dataReceived(self, data):
if (self.client != None):
self.client.write(data)
else:
self.buffer = data
def write(self, data):
self.transport.write(data)
print 'Server: ' + data.encode('hex')
class ClientProtocol(protocol.Protocol):
def connectionMade(self):
self.factory.server.client = self
self.write(self.factory.server.buffer)
self.factory.server.buffer = ''
def dataReceived(self, data):
self.factory.server.write(data)
def write(self, data):
self.transport.write(data)
print 'Client: ' + data.encode('hex')
def main():
factory = protocol.ServerFactory()
factory.protocol = ServerProtocol
reactor.listenTCP(8080, factory)
reactor.run()
if __name__ == '__main__':
main()
In class ServerProtocol, After the connection (request) is made , I am forwarding the request to remote IP x.x.x.x on port 80. Now, I want to loadbalance the incoming requests to multiple remote IPs( x.x.x.x, y.y.y.y, z.z.z.z) on port 80. I looked at txLoadBalancer, but could't find any example to fit this in.
Is there any other way I can do this using twisted framework?
(Note:- I want to programmatically loadbalance the requests, please don't suggest me to use third party loadbalancers)