The error you’re seeing means that the host (domain name or IP address) you are trying to connect to cannot be resolved by the DNS resolver on your machine.

socket.gaierror: [Errno 8] nodename nor servname provided, or not known

Why This Happens

This error generally occurs in Python when you’re trying to make a network request using a bad or undefined hostname:

Common Causes:

1. Typo in the hostname or URL

socket.gethostbyname("example.com")  # typo: 'htp' instead of 'http'

2. Host is empty or None

socket.gethostbyname("")  # Empty host string

3. Network issues or no internet connection

4. Trying to resolve a private/local hostname that doesn’t exist

5. Invalid port or malformed address

6. Improper proxy setup

How to Fix It

1. Check the host string
Make sure you are passing a valid and correctly spelled hostname, e.g.:

socket.gethostbyname("google.com")  # âś…

2. Avoid passing None or empty string
Ensure variables aren’t empty:

host = "example.com"
if host:
    socket.gethostbyname(host)
else:
    print("Invalid host")

3. Debug with print()
If you’re using requests or a library:

print("Connecting to:", url)  # Make sure it's valid

4. Check your internet connection
Try ping google.com in your terminal to verify DNS is working.

5. If you’re using requests or urllib

Bad:

requests.get(some-host")  # typo or non-existent host

Fix:

requests.get("example.com")

Example Fix

Before (may cause the error):

host = ""
socket.gethostbyname(host)

After (safe usage):

import socket
try:
    ip = socket.gethostbyname("example.com")
    print("IP Address:", ip)
except socket.gaierror as e:
    print("Hostname resolution failed:", e)

You’re encountering a known issue with socket.gethostbyname(socket.gethostname()) on macOS Sierra and later. Here’s what’s happening:

Why It Worked on El Capitan (but not Sierra+)

  • On OS X El Capitan, socket.gethostname() typically returned a resolvable hostname (one that DNS could map to an IP address).
  • On macOS Sierra and later, socket.gethostname() often returns a local name that’s not resolvable (like MacBook-Pro.local or something without a DNS entry), causing socket.gethostbyname() to fail with:
gaierror: [Errno 8] nodename nor servname provided, or not known

Best Cross-Platform Alternative

If you’re trying to get your local IP address, use this method instead:

import socket
def get_local_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # Doesn't even need to be reachable
        s.connect(("8.8.8.8", 80))
        ip = s.getsockname()[0]
    except Exception:
        ip = "127.0.0.1"
    finally:
        s.close()
    return ip
print(get_local_ip())

 

Output:

Returns your actual local IP address, like:

192.168.1.12

This method:

  • Works across all modern macOS versions
  • Doesn’t depend on DNS or local hostname resolution
  • Is fast and reliable

Optional Fix: Edit /etc/hosts (Not Recommended)

You could manually add a line to /etc/hosts like:

127.0.0.1   MacBook-Pro.local

but this is not recommended, as it’s brittle and can break networking.

Also Read

Also Read:

Node JS Multer

Need Help With Node Development?

Work with our skilled Node developers to accelerate your project and boost its performance.

Hire Node.js Developers

Support On Demand!

Related Q&A