Proxy Webserver in Python

Posted by  Franco Falkov
 584  View(s)
Rate this:

How to Create a Proxy Webserver in Python ?

  1. Re: Proxy Webserver in Python

    Socket programming in python is very user friendly user. user focusing on the application layer more than the network layer in python. 

    Creating an incoming socket : 

    *create a socket serverSocket in the __init__ method of the Server Class. 

    def __init__(self, config): 
        # Shutdown on Ctrl+C
        signal.signal(signal.SIGINT, self.shutdown)
        # Create a TCP socket
        self.serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # Re-use the socket
        self.serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        # bind the socket to a public host, and a port
        self.serverSocket.bind((config['HOST_NAME'], config['BIND_PORT']))
        self.serverSocket.listen(10) # become a server socket
        self.__clients = {}

    Accept client and process:

    while True: 

        # Establish the connection 
        (clientSocket, client_address) = self.serverSocket.accept()
        d = threading.Thread(name=self._getClientName(client_address),
        target = self.proxy_thread, args=(clientSocket, client_address))

    Redirecting the traffic:

    # get the request from browser 
    request = conn.recv(config['MAX_REQUEST_LEN'])
    # parse the first line
    first_line = request.split('\n')[0]
    # get url
    url = first_line.split(' ')[1]

    How to test the server?

    - Blacklisting Domains 

    - Content monitoring

    - Logging

    - HTTP WebServer + ProxyServer