Quick Tips to Tackle the Error “socket.gaierror: [errno -2] name or service not known”

Python plays an important role when it comes to programming and designing amazing web pages and applications. Programmers all over the world are using Python programming language to build unique projects and make their mark in the world of programming. Syntax of Python is quite simple as it is written in the English language, and that’s the reason beginners are also trying small projects to become a pro and set their foot in programming. Various projects can be designed, and as it is the most frequent language, it is obvious to encounter errors, and “socket.gaierror: [errno -2] name or service not known” is one of those errors that you get into.

The socket.gaierror occurs when the error is caught by the block while searching for information about IP addresses. You don’t need to worry when you encounter the error warning as it is not as complex as it seems. We have ideal solutions to help you fix the error. Go ahead with the post to remove the error. Let’s check out how the error occurs

How do you get the error warning?

The moment you start trying to integrate SMTP to get mails, you get the following error

File "C:\Users\Ubaid Parveez\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 336, in connect
  self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Users\Ubaid Parveez\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 307, in _get_socket
  File "C:\Users\Ubaid Parveez\AppData\Local\Programs\Python\Python36\lib\socket.py", line 705, in create_connection
  for res in getaddrinfo(host, port, 1, SOCK_STREAM):
  File "C:\Users\Ubaid Parveez\AppData\Local\Programs\Python\Python36\lib\socket.py", line 748, in getaddrinfo
  for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
 socket.gaierror: [Errno 10047] getaddrinfo failed

This is what you get in return. Check out the program code you run

SERVER_EMAIL = '[email protected]'
 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
 EMAIL_HOST = 'smtp.gmail.com'
 EMAIL_HOST_PASSWORD = 'password_here'

The view that sends the email

class ContactView(generic.TemplateView):
  template_name = "contact.html"
  def post(self,request,*args,**kwargs):
  'Subject here',
  'Here is the message.',
  ['[email protected]'],

Now that you know how you land up in trouble, check out the solution to handle the exception

Solutions To Remove The Error “socket.gaierror: [errno -2] name or service not known”

We have a few solid options to help you get rid of the error warning.

Option 1 – Socket programming

To solve the error, socket programming works. It is basically known for connecting two nodes to link or communicate on a network. In socket programming, one socket (also known as a node) follows the command on a specified port at an IP, whereas another one tries to reach out to the other in order to form a connection.  The client can reach the server and the listener socket is created by the server. This is known as an integral part of web browsing.  Here, we have a client and a server. The socket library is imported in socket programming and making a simple socket is a part of it. Check out how you do it

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

To take a reference, have a look at the below code

# An example script to connect to Google using socket
# programming in Python
import socket # for socket
import sys

   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   print ("Socket successfully created")
except socket.error as err:
   print ("socket creation failed with error %s" %(err))
# default port for socket
port = 80

   host_ip = socket.gethostbyname('www.google.com')
except socket.gaierror:
   # this means could not resolve the host
   print ("there was an error resolving the host")

# connecting to the server
s.connect((host_ip, port))

print ("the socket has successfully connected to google")

Option 2 – Configure /etc/gai.conf

You get the error warning because the name resolutions for a particular domain are dropping. The reason for the name resolution to fail is that your internet may have disconnected. The glibc uses the GetAddressInfo (gai) in order to perform name resolutions, which is considered the part of glibc and is used by the socket. It can be configured in /etc/gai.conf. In this way, you get to remove the error warning.


And here we are with the solutions to fix the error “socket.gaierror: [errno -2] name or service not known”. I hope you find it helpful! I wish you luck!

Leave a Reply

Your email address will not be published. Required fields are marked *