Django — AWS — Servname not supported for ai_socktype

Rahmat Ramadhan Irianto
2 min readFeb 27, 2021

My friend who is a sysadmin just came to me and ask for help with his issues when deploy the server. He is getting stuck about this kind of error for almost 3 days. The system running Django with Docker and hosted on AWS EC2.

At first, we spot the error when we try to send emails using Django send_mail, I checked the code and settings and everything was fine. Since I have using this send_mail lib for almost all of my projects.

We also try to change the EMAIL_HOST and other email config but no luck.

for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File “/usr/local/lib/python3.7/socket.py”, line 752, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -8] Servname not supported for ai_socktype***

we can see that the problem goes at the getaddrinfo() section, https://man7.org/linux/man-pages/man3/getaddrinfo.3.html

If we read about getaddrinfo we will know that the getaddrinfo() function is used to get a list of IP addresses and port numbers for host hostname and service servname.

And the servname is either a decimal port number or a service name listed in services(5).

So let's check the hostname and if the hostname is available on /etc/hosts

>>> import socket
>>> socket.gethostname()
'b3fc175acd32'
# cat /etc/hosts
127.0.0.1 localhost
...
172.10.0.5 b3fc175acd32

Yes, it's available! and now let's check the port on /etc/services

# cat /etc/services | grep 2525/tcp

It shows an empty result, where the port was not defined in /etc/services and this was causing the error. The system didn’t know how to make SMTP connections without that.

So, Let’s check the default port of SMTP

cat /etc/services | grep 587/tcp
submission 587/tcp # Submission

Yes, it's there!

So, we finally solved this by changing the port to 587 in the email settings as the default SMTP port 😆 https://tools.ietf.org/html/rfc2476#section-3.1

Things to note that before using an unusual port, makes sure that the port is already defined and not in conflict with other services. And don’t forget to add the following lines to /etc/services if the port is not listed.

smtp_alt 2525/tcp # new SMTP port

Hope this helps! Please give me some feedback!

--

--

Rahmat Ramadhan Irianto

I’m a Python & Js Enthusiast. @BlockchainSpace BI Solution Architect. This is about my dev journal #Github https://github.com/rririanto