Click to See Complete Forum and Search --> : AIM proxy server in python


inkedmn
12-03-2002, 01:13 AM
# lightweight proxy server for AIM

import asyncore
import socket
import sys

aimAddr = "login.oscar.aol.com"
aimPort = 5190

class AimProxy(asyncore.dispatcher):
"""Listens for new client connections and creates new ToClient
objects for each one."""

def __init__(self, clientPort):
"""Creates the socket, binds to clientPort"""
asyncore.dispatcher.__init__(self)
self.clientPort = clientPort
self.host = ""
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.bind((self.host, self.clientPort))

def handle_accept(self):
"""Handles new client connections"""
conn, addr = self.accept()
print addr, "connected."
return ToClient(conn)

def handle_close(self):
self.close()
print "AimProxy socket closed"
sys.exit(0)

def run(self):
print "AIM Proxy server running..."
self.listen(5)

def die(self, error):
print "Error: %s" % error
print "Forcing shutdown..."
self.handle_close()


class ToClient(asyncore.dispatcher_with_send):
def __init__(self, sock):
asyncore.dispatcher_with_send.__init__(self, sock)
aimSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
aimSock.connect((aimAddr, aimPort))
self.aim = ToAim(self, aimSock)

def handle_read(self):
data = self.recv(1024)
self.aim.send(data)

def handle_close(self):
print "Closing client socket..."
self.close()


class ToAim(asyncore.dispatcher_with_send):
def __init__(self, sock, aimSock):
asyncore.dispatcher_with_send.__init__(self, aimSock)
self.client = sock

def handle_read(self):
data = self.recv(1024)
self.client.send(data)

def handle_close(self):
print "Closing AIM socket..."
self.close()


if __name__ == '__main__':
try:
proxy = AimProxy(1234)
proxy.run()
asyncore.loop()
except Exception, e: pass


enjoy :)