Proxy Webserver in Python

How to Create a Proxy Webserver in Python ?

Last updated:7/13/2018 4:47:00 AM

1 Answers

Prakash nidhi Verma
Prakash nidhi Verma

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