summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDenis Bilenko <denis@ag-projects.com>2008-12-10 16:35:35 +0600
committerDenis Bilenko <denis@ag-projects.com>2008-12-10 16:35:35 +0600
commit73486bd5e6949e8e4fe99ec8ed36ad35f1739a52 (patch)
tree2ae49c0d74948fd8ce5aa45229e7052f50dc541c /examples
parentaad4bba29daf1e2cb0e309e10792f89d38f0abdd (diff)
downloadeventlet-73486bd5e6949e8e4fe99ec8ed36ad35f1739a52.tar.gz
added examples/twisted_portforward.py
Diffstat (limited to 'examples')
-rw-r--r--examples/twisted_portforward.py30
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()