Hello. It’s been a while.

The problem: localhost is unreachable

While following this this tutorial on how to setup a flask API i encountered an error while attempting to PUT or POST data;

$ curl http://localhost:5000/todo1 -d "data=Remember the milk" -X PUT

The error

Python 2.7.18 (default, Mar  8 2021, 13:02:45) 
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from requests import put, get
>>> put('http://localhost:5000/todo1', data={'data': 'Remember the milk'}).json()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  ...
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost',
port=5000): Max retries exceeded with url: /todo1 
(Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at
 0x7f56d6bd0dd0>: Failed to establish a new connection: [Errno 111] Connection
  refused',))

Attempt 1

Now I was using the same virtual environment to GET and POST data from the site. I thought that using /usr/bin/python for one method and the virtual environment for the other would solve the issue. But it did not.

Attempt 2

While typing out localhost in my browser’s addressbar customName (not excatly but it was my own custom name) popped-up on the list and the idea of substituting customName for localhost presented itself.

I confirmed this using usr/bin/python and also from my virtualenv;

using my machine’s python

$yourname@customName curl http://customName:5000/todo1 -d "data=Remember the milk" -X PUT
{
    "todo1": "Remember the milk"
}

from the virtualenv

$yourname@customName curl http://customName:5000/todo1 -d "data=Remember the milk" -X PUT
{
    "todo1": "Remember the milk"
}

Things to keep in mind

$vi /etc/hosts
127.0.0.1 customName
127.0.1.1 localhost

# The following lines are desirable for IPv6 capable hosts
...

Happy coding!