I started studying the socket
library in Python for the first time, and I realized that there is a constant called AF_UNIX
that does the same thing as AF_INET
but it is faster and establishes connection only with the same machine and uses the system file instead of internet connection
but what I didn't understand is why? why would i establish communication with my own machine? what use would that be and in what situation would it really be useful to use AF_UNIX?
Your question has many answers, I'll try to answer a few use cases for AF_UNIX
:
You could use it to expose a private channel for other processes to communicate securely using files (since it'll respect the filesystem hierarchy and *nix permission model). PostgreSQL is known to expose a unix socket for it's psql
cli to connect securely.
You could implement exchangeable communications using a socket protocol, as it's easy to move between AF_INET
and AF_UNIX
and expand it's communication throughout different hosts (redis and docker does this).
You could create a extendable and fast way for different processes and technologies to communicate without the device overhead AF_INET
would include, even if you're using loopback interface.
The possibilities are actually limitless, as it includes a bit of personal preference. But the fact that AF_UNIX
uses a similar specification as AF_INET
makes it a powerful tool to extend communications between processes without having to rely on heavily different tools (FIFOs, shared mem, internal queues, files, to name a few).