HTTP redirect customization using Python requests

I have the following scenario: (using requests module in Python 3.7.2)

  • HTTP server (server-1) is in Docker container in the default bridge network on MacOS. The http port 80 is mapped to a host port 8001.

  • HTTP client is on MacOS host, querying the server using the mapped host port (8001).

  • The server-1 returns a HTTP redirect to another http server (server-2) in another Docker container (the same bridge network) . The redirect url is http://server-2:80/ . The server-2 has its port 80 also mapped to host port 8002.

Now the problem is that the client cannot reach to the redirect URL due to Docker constraints. And by default the Python requests handles redirect automatically and sends the query to the redirect URL.

My question is: is there any way for the client to inspect / customize the redirect URL from requests module before it sends off?

Source: StackOverflow