To run the proxy server the following files are required
- main.py
- proxy.py
- request.py
- response.py
- timeparser.py
You also need Python installed on your machine.
First we create an instance of the proxy, this can be done in the following way:
proxy = Proxy("127.0.0.1", 9999, 10)
When a new proxy-server is created, three values are required. The first value is the IP-address where the proxy will be hosted. The second value is which port the proxy is going to use. The last value is the maximum numbers of connections in the queue waiting to be accepted.
When an instance of a proxy-server is created, it can be configured to match your requirements:
proxy.use_cache = True
If use_cache is set to True the proxy will use its own cache to retrieve websites you have already visited without sending a new requests to the server.
proxy.keep_alive = False
If keep_alive is set to False the header "Connection:" will be set to "close" in all requests. Otherwise it is set to "keep-alive".
proxy.add_request_replacement("match", "replacement")
The first parameter "match" should be a regular expression and everything that matches that pattern will be replaced with the parameter "replacement". The proxy will only replace content in the first line of the request when using this method.
proxy.add_request_replacement("match", "replacement")
The first parameter "match" should be a regular expression and everything that matches that pattern will be replaced with the parameter "replacement". The proxy will replace everything in the response-body.
When the proxy is configured to match your requirements, call the method run to start the server.
proxy.run()
Run the file "unit_test.py" to run all existing unit tests.
This is how you could configure the proxy server to do the following thing:
- Replace all jpg and png-images to a trollface-picture.
from proxy import Proxy
if __name__ == "__main__":
proxy = Proxy("127.0.0.1", 9999, 10)
proxy.keep_alive = False
proxy.use_cache = True
proxy.add_request_replacement("(?<= ).*.[jpg|png]", "https://yourhost.com/trollface.jpg")
proxy.run()
In this example we want to replace all occurences of the word "Alice" to "Trolly".
from proxy import Proxy
if __name__ == "__main__":
proxy = Proxy("127.0.0.1", 9999, 10)
proxy.keep_alive = False
proxy.use_cache = True
proxy.add_response_replacement('(?<=[\s|"])Alice', 'Trolly')
proxy.run()