These links are ordered by their time last-updated -- rightmost is newest.
Show code - CODE/python/proxy.py

#!/usr/bin/env python2
import asynchat
import asyncore
import socket
import string
import sys

BUFFER_SIZE = 1024*8
PROXY_PORT = 6715

class proxy_server (asyncore.dispatcher):
    ''' Set up the listening socket '''    
    def __init__ (self, host, port):
        asyncore.dispatcher.__init__ (self)
        self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.mud = (host, port)
        self.bind (('', PROXY_PORT))
        self.listen (5)
	asyncore.loop()

    def handle_accept (self):
        proxy_accept (self, self.accept())

class proxy_accept (asynchat.async_chat):
    ''' Handle the channel between the client connected and our proxy '''

    channels = 0

    def __init__ (self, server, (conn, addr)):
        asynchat.async_chat.__init__ (self, conn)
        self.set_terminator ('\n')
	self.id = self.channels
	self.channels = self.channels + 1
        self.server = server
        self.uplink = proxy_uplink (self, server.mud)
        self.buffer = ''

    def collect_incoming_data (self, data):
        self.buffer = self.buffer + data

    def found_terminator (self):
        data = self.buffer
        self.buffer = ''
        self.uplink.send (data)

    def handle_close (self):
	self.channels = self.channels - 1
        self.uplink.close()
        self.close()

class proxy_uplink (asyncore.dispatcher):
    ''' Handle the connection to the mud server '''
    write_buffer = ''
    buffer = ''

    def __init__ (self, parent, (conn, addr)):
        asyncore.dispatcher.__init__ (self)
        self.parent = parent
	self.id = parent.id
	self.mud = self.parent.server.mud
	self.host = self.mud[0]
	self.port = self.mud[1]
	self.connect()

    def connect( self ):
        self.create_socket( socket.AF_INET, socket.SOCK_STREAM )
        #self.socket.setblocking(0)
        try:
            asyncore.dispatcher.connect( self, ( self.host, self.port ) )
        except Exception, e:
            errmsg = str( e )
            print "Error ", errmsg
            return

    def handle_read( self ):
        text = self.recv( BUFFER_SIZE )
	self.parent.send( text )

    def handle_write( self ):
        sent = asyncore.dispatcher.send( self, self.write_buffer )
        self.write_buffer = self.write_buffer[sent:]

    def send( self, data ):
        self.write_buffer = self.write_buffer + data + "\n"

        
    def handle_close (self):
        print 'Closing'
        self.close()
	self.parent.close()

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print 'Usage: %s <mud-host> <mud-port>' % sys.argv[0]
    else:
        proxy_server (sys.argv[1], string.atoi (sys.argv[2]))



This web page and related elements are for informative purposes only and thus the use of any of this information is at your risk! In accordance with Title 17 U.S.C. Section 107 and The Berne Convention on Literary and Artistic Works, Article 10, news clippings on this site are made available without profit for research and educational purposes. Any trademarks, trade names, service marks, or service names used on this site are the property of their respective owners.