Commit 8a5f59e3 authored by Denis Bilenko's avatar Denis Bilenko

add examples/portforwarder.py

parent c422296b
import sys
import gevent
from gevent.server import StreamServer
from gevent.socket import create_connection, gethostbyname
class PortForwarder(StreamServer):
def __init__(self, listener, dest, **kwargs):
StreamServer.__init__(self, listener, **kwargs)
self.dest = dest
def handle(self, source, address):
log('%s:%s accepted', *address[:2])
dest = create_connection(self.dest)
forwarder1 = gevent.spawn(forward, source, dest)
forwarder2 = gevent.spawn(forward, dest, source)
gevent.joinall([forwarder1, forwarder2], count=1)
def forward(source, dest):
source_address = '%s:%s' % source.getpeername()[:2]
dest_address = '%s:%s' % dest.getpeername()[:2]
try:
while True:
data = source.recv(1024)
log('%s->%s: %r', source_address, dest_address, data)
if not data:
break
dest.sendall(data)
finally:
source.close()
dest.close()
def parse_address(address):
try:
hostname, port = address.rsplit(':', 1)
port = int(port)
except ValueError:
sys.exit('Expected HOST:PORT: %r' % address)
return gethostbyname(hostname), port
def main():
args = sys.argv[1:]
if len(args) != 2:
sys.exit('Usage: %s source-address destination-address' % __file__)
source = args[0]
dest = parse_address(args[1])
server = PortForwarder(source, dest)
log('Starting port forwarder %s:%s -> %s:%s', *(server.address[:2] + dest))
server.serve_forever()
def log(message, *args):
message = message % args
sys.stderr.write(message + '\n')
if __name__ == '__main__':
main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment