socket.gaierror: [Errno 11001] getaddrinfo failed [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. socket.gaierror: [Errno 11001] getaddrinfo failed
  2. The error commonly occurs when you try to connect from behind a proxy
  3. Trying to use pip install from behind a proxy
  4. Make sure you don't have internet connectivity issues

# socket.gaierror: [Errno 11001] getaddrinfo failed [Solved]

The article addresses the following 2 related errors:

  • socket.gaierror: [Errno 11001] getaddrinfo failed
  • socket.gaierror: [Errno -2] Name or service not known

The Python "socket.gaierror: [Errno 11001] getaddrinfo failed" occurs for 2 main reasons:

  1. Trying to resolve a hostname incorrectly. Your Python application cannot resolve the IP address of the host.
  2. Incorrectly setting the http_proxy environment variable.
  3. Having internet connectivity issues.

If you get the error when using the socket.getaddrinfo() method, make sure you haven't passed it incorrect arguments.

For example, if you're trying to connect to a server running on a localhost port, your getaddrinfo() call would look something like this:

main.py
import socket # [(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('127.0.0.1', 8000)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('127.0.0.1', 8000)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('127.0.0.1', 8000))] print(socket.getaddrinfo('localhost', 8000))

calling socket getaddrinfo correctly

The first argument we passed to the method is the host and the second is the port.

If that doesn't work, you can also try to replace localhost with the string '127.0.0.1'.

main.py
import socket # [(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('127.0.0.1', 8000)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('127.0.0.1', 8000)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('127.0.0.1', 8000))] print(socket.getaddrinfo('127.0.0.1', 8000))

The string 127.0.0.1 is equivalent to localhost but resolves more often.

You can also call the socket.getaddrinfo() method with a fully qualified domain name.

main.py
import socket print(socket.getaddrinfo('google.com', 443))

call socket getaddr info with fully qualified domain name

However, make sure the protocol scheme is not included because that causes an error.

main.py
import socket # ⛔️ socket.gaierror: [Errno -2] Name or service not known print(socket.getaddrinfo('https://google.com', 443))

make sure protocol scheme is not included

Including the protocol scheme (https:// or http://) causes an error.

If you need to handle the potential error, use a try/except block.

main.py
import socket try: print(socket.getaddrinfo('google.com', 443)) except socket.gaierror as e: print('An error occurred: ', e)

We try to call the socket.getaddrinfo() method in the try block.

If a socket.gaierror exception occurs, it gets handled in the except block.

# The error commonly occurs when you try to connect from behind a proxy

The error also commonly occurs when you try to connect to a network from behind a proxy.

You might have set the http_proxy or https_proxy environment variables by mistake.

You can print the values of the variables by using the following commands on Windows.

shell
# Windows CMD echo %http_proxy% echo %https_proxy%

If you are on macOS or Linux, issue the following commands instead.

shell
# macOS and Linux echo $http_proxy echo $https_proxy

To clear the http_proxy and https_proxy environment variables on Windows, issue the following commands in CMD (Command Prompt).

cmd
# for Windows CMD (Command Prompt) set http_proxy=null set https_proxy=null set HTTP_PROXY=null set HTTPS_PROXY=null

If you use PowerShell on Windows, issue the following commands instead.

PowerShell
# for Windows CMD (PowerShell) [Environment]::SetEnvironmentVariable('http_proxy', '', 'User') [Environment]::SetEnvironmentVariable('http_proxy', '', 'Machine') [Environment]::SetEnvironmentVariable('https_proxy', '', 'User') [Environment]::SetEnvironmentVariable('https_proxy', '', 'Machine')

If you are on macOS or Linux, issue the following commands in bash or zsh.

shell
# for macOS, Linux or Windows Git Bash unset http_proxy unset https_proxy unset HTTP_PROXY unset HTTPS_PROXY

Check if the error is resolved after removing the http_proxy and https_proxy environment variables.

If you need to use a proxy, try setting the environment variables to the correct value.

For example, if you are on Windows, the commands would look something like this:

cmd
# for Windows # without a username and a password set HTTP_PROXY=http://proxy.company:8080 # or with a username and a password set HTTPS_PROXY=http://USERNAME:PASSWORD@proxy.company:8080

If you are on macOS or Linux, the commands would look something like this:

shell
# For macOS or Linux # without a username and a password export http_proxy=http://proxy.company:8080 export HTTP_PROXY=http://proxy.company:8080 # or with a username and a password export https_proxy=http://USERNAME:PASSWORD@proxy.company:8080 export HTTPS_PROXY=http://USERNAME:PASSWORD@proxy.company:8080

Make sure to replace the placeholders with the actual values.

# Trying to use pip install from behind a proxy

If you're trying to use pip install from behind a proxy, set the --proxy parameter.

shell
pip install --proxy http://user:password@proxy_server:port requests

If you don't have to specify the username and password, your command might look something like this:

shell
pip install --proxy http://proxy_server:port

If you set the http_proxy or https_proxy environment variables, you don't have to set the --proxy parameter when running pip install.

# Make sure you don't have internet connectivity issues

Make sure your connection to the internet is not flake, as that also causes the error.

If the server you're trying to connect to requires the use of a VPN, make sure your VPN is turned on.

You can also try to restart your VPN or try to disable it.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.