A simple reverse proxy server for testing purposes. It forwards requests to another server, port and path prefix and returns the response.
The following features are supported:
- Forwarding of HTTP requests to another server
- Configurable target server, port, and path prefix
- Mapping of incoming request paths to target paths
- Delay simulation for testing purposes
- Simulate unreliable network conditions with random failures
Please note that this is a simple implementation intended for testing and development purposes only. It is not suitable for production use.
Download the code and run the following command to install the required dependencies:
pip install -r requirements.txt- Python 3.10 or higher
The proxy server can be run with various options to customize its behavior.
- --port, -p PORT
Port to listen on. Default is 9090. - --paths, -pa PATHS [PATHS ...]
List of path prefixes to map. Default is '/'. - --target-port, -tp TARGET_PORT
Target server port. Default is 8080. - --target-host, -th TARGET_HOST
Target server host. Default is 'localhost'. - --target-paths, -tpa TARGET_PATHS [TARGET_PATHS ...]
Target server path prefix list to map to. Default is '/'. - --target-scheme, -ts {http,https}
Target server scheme (http or https). Default is 'http'. - --delay, -d DELAY
Artificial delay in ms before forwarding each request. Default is 0 (no delay). - --fail-rate, -fr FAIL_RATE
Simulated failure rate (0.0 to 1.0). Default is 0.0 (no failures). - --fail-type, -ft {unavailable,timeout,unreachable}
Type of simulated failure. Default is 'unavailable'. - --verbose, -v
Enable verbose output. Default is false.
Both the --paths and --target-paths options accept multiple values to map different path prefixes. The number of entries in both lists must match.
To run the proxy server to forward requests, use the following command:
python proxy.py --target-host <TARGET_HOST> --target-port <TARGET_PORT>python proxy.py --target-host example.com --target-port 80To forward requests with path mapping, use the --path-mapping option:
python proxy.py --target-host <TARGET_HOST> --target-port <TARGET_PORT> --paths <Path at proxy> --target-paths <PATH_MAPPING>This will map requests from <Path at proxy> to <PATH_MAPPING> on the target server.
python proxy.py --target-host example.com --target-port 80 --paths /api --target-paths /v1/apiTo simulate network delays and failures, use the --delay, --fail-rate, and --fail-type options:
python proxy.py --target-host <TARGET_HOST> --target-port <TARGET_PORT> --delay <DELAY_IN_MS> --fail-rate <FAILURE_RATE> --fail-type <FAILURE_TYPE>To introduce a 1000 ms delay before forwarding each request:
python proxy.py --target-host example.com --target-port 80 --delay 1000To simulate a 10% failure rate with timeout failures:
python proxy.py --target-host example.com --target-port 80 --fail-rate 0.1 --fail-type timeoutThe proxy server automatically adjusts Location headers in HTTP responses to ensure that redirects point back to the proxy server rather than the target server. This is particularly useful when the target server issues redirects, and you want clients to continue interacting with the proxy.