Added port forwarder example.

This commit is contained in:
Ryan Williams
2010-02-28 14:30:34 -08:00
parent f1030597a2
commit 4e7b1b7b23
2 changed files with 31 additions and 1 deletions

View File

@@ -54,4 +54,10 @@ Feed Scraper
This example requires `Feedparser <http://www.feedparser.org/>`_ to be installed or on the PYTHONPATH.
.. literalinclude:: ../examples/feedscraper.py
.. literalinclude:: ../examples/feedscraper.py
Port Forwarder
-----------------------
``examples/forwarder.py``
.. literalinclude:: ../examples/forwarder.py

24
examples/forwarder.py Normal file
View File

@@ -0,0 +1,24 @@
""" This is an incredibly simple port forwarder from port 7000 to 22 on localhost. It calls a callback function when the socket is closed, to demonstrate one way that you could start to do interesting things by
starting from a simple framework like this.
"""
import eventlet
def closed_callback():
print "called back"
def forward(source, dest, cb = lambda: None):
"""Forwards bytes unidirectionally from source to dest"""
while True:
d = source.recv(32384)
if d == '':
cb()
break
dest.sendall(d)
listener = eventlet.listen(('localhost', 7000))
while True:
client, addr = listener.accept()
server = eventlet.connect(('localhost', 22))
# two unidirectional forwarders make a bidirectional one
eventlet.spawn_n(forward, client, server, closed_callback)
eventlet.spawn_n(forward, server, client)