Write a Reverse Proxy Server in Python: Part 4 (Shell Script for Automation)

3 minute read

In the previous posts, we have coded our Reverse Proxy server, server-side code for the servers in the server pool, and our client-side script.

In this post we will write an automation script and then check the output to find out if our reverse proxy works fine.

Running Scripts with arguments

This is an example of how we are going to run all the scripts

Reverse Proxy Server

$ python reverse_proxy.py -port 8000

Server

python server.py -id 100 -pp 111 -listen 5000 -revproc 8000

Client

python client.py -id 1 -revproc 8000 -pkt pktfiles/1.json

Automation Script

Let’s put all the script in a directory named reverse_proxy and keep the directory in the user home directory. Now, to create a reverse proxy server we run the script with input arguments for once. We will also create four servers in a server pool and six clients in total. Let’s create a file, copy the codes provided below, and then run the shell program in terminal.

Note: Do not forget to add execute permission to your script.

$ nano main.sh
$ chmod +x main.sh

or

$ nano main.sh
$ chmod 777 main.sh

Basically what we are going to achieve is launch new terminal window for each server and client, and then check the output.

macOS terminal

The entire code is available in GitHub.

#!/bin/bash

osascript -e 'tell app "Terminal"
    do script "cd ~/reverse_proxy && python reverse_proxy.py -port 2100"
end tell'

sleep 2

osascript -e 'tell app "Terminal"
    do script "cd ~/reverse_proxy && python server.py -id 100 -pp 111 -listen 2105 -revproc 2100"
end tell'

osascript -e 'tell app "Terminal"
    do script "cd ~/reverse_proxy && python server.py -id 200 -pp 222 -listen 2110 -revproc 2100"
end tell'

osascript -e 'tell app "Terminal"
    do script "cd ~/reverse_proxy && python server.py -id 300 -pp 111 -listen 2115 -revproc 2100"
end tell'

osascript -e 'tell app "Terminal"
    do script "cd ~/reverse_proxy && python server.py -id 400 -pp 222 -listen 2120 -revproc 2100"
end tell'

sleep 2

osascript -e 'tell app "Terminal"
    do script "cd ~/reverse_proxy && python client.py -id 1 -revproc 2100 -pkt pktfiles/1.json"
end tell'

osascript -e 'tell app "Terminal"
    do script "cd ~/reverse_proxy && python client.py -id 2 -revproc 2100 -pkt pktfiles/2.json"
end tell'

osascript -e 'tell app "Terminal"
    do script "cd ~/reverse_proxy && python client.py -id 3 -revproc 2100 -pkt pktfiles/3.json"
end tell'

osascript -e 'tell app "Terminal"
    do script "cd ~/reverse_proxy && python client.py -id 4 -revproc 2100 -pkt pktfiles/4.json"
end tell'

osascript -e 'tell app "Terminal"
    do script "cd ~/reverse_proxy && python client.py -id 5 -revproc 2100 -pkt pktfiles/5.json"
end tell'

osascript -e 'tell app "Terminal"
    do script "cd ~/reverse_proxy && python client.py -id 6 -revproc 2100 -pkt pktfiles/6.json"
end tell'

Linux Terminal

I have not checked the Linux one, but it should work fine. The entire code is available in GitHub.

#!/bin/bash

gnome-terminal -- /bin/bash -c "cd ~/reverse_proxy; python reverse_proxy.py -port 2200; bash"

sleep 2

gnome-terminal -- /bin/bash -c "cd ~/reverse_proxy; python server.py -id 100 -pp 111 -listen 2105 -revproc 2200; bash"
gnome-terminal -- /bin/bash -c "cd ~/reverse_proxy; python server.py -id 200 -pp 222 -listen 2110 -revproc 2200; bash"
gnome-terminal -- /bin/bash -c "cd ~/reverse_proxy; python server.py -id 300 -pp 111 -listen 2115 -revproc 2200; bash"
gnome-terminal -- /bin/bash -c "cd ~/reverse_proxy; python server.py -id 400 -pp 222 -listen 2120 -revproc 2200; bash"

sleep 2

gnome-terminal -- /bin/bash -c "cd ~/reverse_proxy; python client.py -id 1 -revproc 2200 -pkt pktfiles/1.json; bash"
gnome-terminal -- /bin/bash -c "cd ~/reverse_proxy; python client.py -id 2 -revproc 2200 -pkt pktfiles/2.json; bash"
gnome-terminal -- /bin/bash -c "cd ~/reverse_proxy; python client.py -id 3 -revproc 2200 -pkt pktfiles/3.json; bash"
gnome-terminal -- /bin/bash -c "cd ~/reverse_proxy; python client.py -id 4 -revproc 2200 -pkt pktfiles/4.json; bash"
gnome-terminal -- /bin/bash -c "cd ~/reverse_proxy; python client.py -id 5 -revproc 2200 -pkt pktfiles/5.json; bash"
gnome-terminal -- /bin/bash -c "cd ~/reverse_proxy; python client.py -id 6 -revproc 2200 -pkt pktfiles/6.json; bash"

Output

Reverse Proxy Server

Any Server

Any Client

Example Video

That’s it for today. In the later tutorials, we will see how to implement the system in Amazon Web Service (AWS EC2).

The whole tutorial series is listed here:

Leave a Comment