8

I'm using python to make 2 APIs communicate between them, i made tests and both APIs work fine on their own, the problem arises when i try to send a request to API A (made with flask) so A can pass the data to API B(made with django) and then return the result to A again.

When i try to test this endpoint, the response is:

HTTPConnectionPool(host='0.0.0.0', port=7000): Max retries exceeded with url: /verify?0 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fa0b939df50>: Failed to establish a new connection: [Errno 111] Connection refused'))

The test file and the manual test fail in this way, if i make a request to API B with the url that API A is making it returns a ok message.

I'm thinking i'm missing something about the config in API B, but where? and what?

Brief Summary:

  • API A it's made in flask and run locally in a dock container

  • API B it's made with django and has has ALLOWED_HOSTS = ["*"] in the config file settings.py adn running locally too

  • When i make a request to API A to call API B, B never register any event in the console log but API B calling to API A the request goes thru succesfully

davidism
  • 121,510
  • 29
  • 395
  • 339
Progs
  • 1,059
  • 7
  • 27
  • 63
  • Is this on your local machine, running in dev mode (i.e. using `runserver`)? It's not possible to make a request in a request with dev servers. You need to run with separate app servers, using proper production webservers that can handle multiple concurrent connections to do this. – dirkgroten Aug 02 '19 at 16:25
  • @dirkgroten i forgot to add that API B is running locally on dev mode and API A is a docker container – Progs Aug 02 '19 at 16:27
  • How are you planning to deploy these? If this is all running on one machine, remember this is just python and it's much more efficient to just `import` and call a function than to go via `requests` or `urllib` (you'll actually have an HTTP request going *outside* of your machine into the network just to go back *into* your machine and be handled by your webserver + app server). – dirkgroten Aug 02 '19 at 16:29
  • So how is API A contacting API B? What host? – dirkgroten Aug 02 '19 at 16:31
  • @dirkgroten i know, i'm doing this because these API are deployed in different servers in production – Progs Aug 02 '19 at 16:31
  • try giving your machine a proper hostname and edit your hosts file so that your API B can be addressed as a proper host. Since API A is inside a container you cannot use `localhost` (since that's just the container itself) so the request never "leaves" your container. – dirkgroten Aug 02 '19 at 16:34
  • Does this help? https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach – dirkgroten Aug 02 '19 at 16:39
  • Maybe it's too silly .. however, here I am: make sure you run *python manage.py runserver 0.0.0.0:7000" to have django accept connections from outside – Mario Orlandi Aug 07 '19 at 10:09
  • @MarioOrlandi, yes that's how i run the django API, i think the issue with the communication is in docker container config – Progs Aug 07 '19 at 14:56

2 Answers2

5

It turns out that I was trying to connect from the docker API with a 0.0.0.0:port and localhost:port, I only needed to use my public IP.

Progs
  • 1,059
  • 7
  • 27
  • 63
3

To access the server from itself, use http://localhost/ or http://127.0.0.1/.

You can also use http://192.168.X.X to access it from a device on the same network.

You need to get your public IP address, which you can find using ipconfig command on Windows machine.

Then, you can try like this:

http://<public_ip>:<your_port>/

It will show you the server welcome page.

Bando
  • 1,223
  • 1
  • 12
  • 31