diff options
author | Denis Bilenko <denis@ag-projects.com> | 2008-12-10 16:35:35 +0600 |
---|---|---|
committer | Denis Bilenko <denis@ag-projects.com> | 2008-12-10 16:35:35 +0600 |
commit | 73486bd5e6949e8e4fe99ec8ed36ad35f1739a52 (patch) | |
tree | 2ae49c0d74948fd8ce5aa45229e7052f50dc541c /examples | |
parent | aad4bba29daf1e2cb0e309e10792f89d38f0abdd (diff) | |
download | eventlet-73486bd5e6949e8e4fe99ec8ed36ad35f1739a52.tar.gz |
added examples/twisted_portforward.py
Diffstat (limited to 'examples')
-rw-r--r-- | examples/twisted_portforward.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/examples/twisted_portforward.py b/examples/twisted_portforward.py new file mode 100644 index 0000000..95d22e5 --- /dev/null +++ b/examples/twisted_portforward.py @@ -0,0 +1,30 @@ +import sys +from twisted.internet import reactor +from eventlet.coros import event, spawn_link +from eventlet.twistedutil import join_reactor +from eventlet.twistedutil.protocol import GreenClientCreator, SpawnFactory, UnbufferedTransport + +def forward(from_, to): + try: + while True: + x = from_.recv() + if not x: + break + print 'forwarding %s bytes' % len(x) + to.write(x) + finally: + to.loseConnection() + +def handler(local): + remote = GreenClientCreator(reactor, UnbufferedTransport).connectTCP(remote_host, remote_port) + error = event() + a = spawn_link(forward, remote, local) + b = spawn_link(forward, local, remote) + a.wait() + b.wait() + +local_port, remote_host, remote_port = sys.argv[1:] +local_port = int(local_port) +remote_port = int(remote_port) +reactor.listenTCP(local_port, SpawnFactory(handler)) +reactor.run() |