Debian Codegraph @

examples/forwarder.py

raw
1 """ This is an incredibly simple port forwarder from port 7000 to 22 on
2 localhost. It calls a callback function when the socket is closed, to
3 demonstrate one way that you could start to do interesting things by
4 starting from a simple framework like this.
5 """
6
7 import eventlet
8
9
10 def closed_callback():
11 print("called back")
12
13
14 def forward(source, dest, cb=lambda: None):
15 """Forwards bytes unidirectionally from source to dest"""
16 while True:
17 d = source.recv(32384)
18 if d == '':
19 cb()
20 break
21 dest.sendall(d)
22
23 listener = eventlet.listen(('localhost', 7000))
24 while True:
25 client, addr = listener.accept()
26 server = eventlet.connect(('localhost', 22))
27 # two unidirectional forwarders make a bidirectional one
28 eventlet.spawn_n(forward, client, server, closed_callback)
29 eventlet.spawn_n(forward, server, client)