import socket




class Server_client():
	def __init__(self, HOST, PORT):
		self.s_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.s_client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
		self.s_client.bind((HOST, PORT))
		print "Serving client HTTP on port %s ..." % PORT
		self.s_client.listen(5)
		print "Wainting for connection... listening on port", PORT
		self.c_client, self.addr_client = self.s_client.accept()


	def handle_client(self):
		buffer = ""
		self.c_client.send("Type something and press enter \n")
		self.data = self.c_client.recv(1024)
		buffer += self.data
		print(buffer)
		self.c_client.close() 
		return buffer
		
		

class Proxy():
	def __init__(self, remote_host, remote_port):
		self.remote_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.remote_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
		self.remote_socket.bind((remote_host, remote_port))
		print "Serving remote server on port %s... " % remote_port
		self.remote_socket.listen(5)
		print "Listening the remote server"
		self.remote_client, self.remote_addr = self.remote_socket.accept()
		
		

	def handle_proxy(self, buffer):
		print "Inside handle proxy"
		print(buffer)
		self.remote_client.send(buffer)
		self.remote_client.close()



if __name__ == '__main__':
	while True:
		client =  Server_client("", 8888)
		buffer = client.handle_client()
		a = Proxy("", 9999)
		a.handle_proxy(buffer)
	
	
