Difference between localhost and the ip address


If bind a socket in Python to localhost,8200 this is accessible via http://localhost:8200/. but not http://x.x.x.x:8200/

If bind a socket in Python to x.x.x.x,8200, machinename,8200 this is accessible via http://x.x.x.x:8200/ and http://machinename:8200/, but not http://localhost:8200/

I thought localhost meant 'this machine', and that using it would 'loop back' to the machine's ip address, but it appears to be different ips.

Is always a different ip address to the machines ip address?


I understand that the actual numbers are different, but what does the loopback do?

For example wikepedia says that
'pointing a web browser to the URLs or http://localhost/ will access that computer's own web site'

but that web site will also be accessible on x.x.x.x, and presumably be set up with a single socket on x.x.x.x. So how do both references work then?


I think I finally understand that and x.x.x.x work like this

-- ==\
               >- Computer
--x.x.x.x   ==/

and not

-- ==\
--------------- x.x.x.x >- Computer


--x.x.x.x ==\
--------------- >- Computer

Thanks all for the help

Best Answer

To answer your question. is not just a different ip address to the machine ip address, it's a different interface as well. should not be seen on the local network. It's a special internal IP address for the loopback adapter.

x.x.x.x will be your ethernet card.

by the way 'localhost' is simply an entry in your hosts file that points to

If you bind a socket to then only clients running locally will be able to access it and only with or http://localhost:8200 (or a dns or hosts file entry that points to

A socket bound to x.x.x.x:8200 will only be available through http://x.x.x.x:8200 or a dns or hosts file entry that points to that.

Binding a socket to does not automatically make it available on the ethernet interface as well.

Thats why you can't connect to x.x.x.x:8200

You can bind port 8200 to all available interfaces ( then it should work.

Other features of the loopback adapter:

The loopback interface is a virtual interface. It's often used to allow programs to talk to each other on the same machine. It is not a way to reference the machine IP address. IS the IP address of the loopback virtual adaptor.

The other thing about the loopback adapter is that because it's virtual it doesn't actually pass any packets through to the network card.