Proxy Webserver in Python

Total Post:113

Points:791
 394  View(s)
Ratings:
Rate this:

How to Create a Proxy Webserver in Python ?

  1. Post:140

    Points:984
    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))
        d.setDaemon(True)
        d.start()


    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

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.