Reputation: 856
I need to write simple but effective and scalable TCP proxy, that would accept connections on one IP:PORT and forward the data to, possibly large, number of clients listening on their IP:PORT pairs.
The question is, if there is a reason to use Twisted here? Will there be any gain, I cant see now, but will see upon project completion? I am asking this question, mainly because Twisted is very complex, and its docs arent newbies friendly - I have the code more or less done using pure Python sockets and multiprocess module, and getting the same written in Twisted already gave me a solid headache.
So, what are your thought on that? Is Twisted a waste of time and overkill, or will it bring some benefits (speed, robustness, scalability and so on)? Maybe there is something else that could be used with greater ease and same success, like Gevent or Eventlet frameworks?
Upvotes: 1
Views: 977
Reputation: 31890
Twisted will make your code simpler, as it already does a lot of the work for you. For example, here is an echo server using Twisted:
from twisted.internet.protocol import Protocol
class Echo(Protocol):
def dataReceived(self, data):
self.transport.write(data)
And here's a round-robin TCP load balancer (untested, but should be at least close to correct):
from twisted.internet.protocol import Factory
from twisted.protocols.portforward import ProxyServer, ProxyFactory
class Balancer(Factory):
def __init__(self, hostports):
self.factories = []
for (host, port) in hostports:
self.factories.append(ProxyFactory(host, port))
def buildProtocol(self, addr):
nextFactory = self.factories.pop(0)
self.factories.append(nextFactory)
return nextFactory.buildProtocol(addr)
Is your existing multiprocess code this simple?
If so, there's still the fact that Twisted will work with platform-specific scalability mechanisms (kqueue on MacOS/BSD, epoll on linux, IOCP on Win32), so you can tune your code to the most appropriate mechanism by using command-line tools, rather than having to actually rewrite your code.
Upvotes: 4