Author : Samual M. Rushing [email protected] [1999-04-30]
Rewrite : scz [email protected] [2006-02-21 18:05]
import asynchat import asyncore import socket import string
class proxy_server ( asyncore.dispatcher ) :
def __init__ ( self, localport, host, port ) :
asyncore.dispatcher.__init__( self )
self.create_socket( socket.AF_INET, socket.SOCK_STREAM )
self.set_reuse_addr()
self.remote = ( host, port )
self.bind( ( '', localport ) )
self.listen( 5 )
def handle_accept ( self ) :
proxy_receiver( self, self.accept() )
class proxy_receiver ( asynchat.async_chat ) :
def __init__ ( self, server, ( client, addr ) ) :
asynchat.async_chat.__init__( self, client )
self.sender = proxy_sender( self, server.remote )
def handle_read ( self ) :
self.sender.push( self.recv( 4096 ) )
def handle_close ( self ) :
self.sender.close()
self.close()
class proxy_sender ( asynchat.async_chat ) :
def __init__ ( self, receiver, address ) :
asynchat.async_chat.__init__( self )
self.receiver = receiver
self.create_socket( socket.AF_INET, socket.SOCK_STREAM )
self.connect( address )
def handle_connect ( self ) :
pass
def handle_read ( self ) :
self.receiver.push( self.recv( 4096 ) )
def handle_close ( self ) :
self.receiver.close()
self.close()
if 'main' == name :
import sys
import string
if 4 != len( sys.argv ) :
print 'Usage: %s ' % sys.argv[0]
else:
ps = proxy_server
(
string.atoi( sys.argv[1] ),
sys.argv[2],
string.atoi( sys.argv[3] )
)
try :
asyncore.loop()
except :
sys.exit(1)