Analysis and Solutions for getaddrinfo Failed Error

Nov 14, 2025 · Programming · 12 views · 7.8

Keywords: Python | socket.getaddrinfo | network error

Abstract: This article provides an in-depth analysis of the common getaddrinfo failed error in Python programming, typically caused by hostname resolution failures. Through Bottle framework example code, it demonstrates error scenarios, explains the working principle of socket.getaddrinfo function, and offers multiple solutions including using IP addresses instead of hostnames and checking network configurations. The article also explores the specific meanings of error codes 11004 and 11001, along with practical debugging methods.

Error Phenomenon and Background

In Python network programming, developers often encounter the gaierror: [Errno 11004] getaddrinfo failed error. This error typically occurs when using web frameworks like Bottle, especially when running simple "Hello World" example programs. The error message indicates that the system cannot resolve the specified hostname, leading to network connection failures.

Core Problem Analysis

getaddrinfo is a critical function in Python's socket module responsible for converting hostnames and service names into address information. When this function fails, it usually indicates DNS resolution issues. In the example, using localhost as the hostname may fail due to system configuration problems.

The hostname resolution functionality can be tested with the following code:

import socket
try:
    result = socket.getaddrinfo('localhost', 8080)
    print("Resolution successful:", result)
except socket.gaierror as e:
    print(f"Resolution failed: {e}")

Solution Approaches

The most direct solution is to use IP addresses instead of hostnames. Replacing localhost with 127.0.0.1 bypasses the DNS resolution process:

import socket
try:
    result = socket.getaddrinfo('127.0.0.1', 8080)
    print("IP address resolution successful")
except socket.gaierror as e:
    print(f"Still failed: {e}")

Understanding Error Codes

Error code 11004 and the referenced 11001 in the article both belong to address information retrieval failures. These errors can be caused by various factors:

Network Configuration Checks

Beyond code modifications, system network configuration should be verified. In Windows systems, DNS resolution can be tested by executing nslookup localhost in the command prompt. If this command also fails, it indicates system-level network configuration problems.

Specific Application in Bottle Framework

In the Bottle framework, localhost is used as the default server address. If resolution fails, explicitly specifying the IP address is recommended:

from bottle import route, run

@route('/')
def hello():
    return "Hello World!"

# Use IP address instead of localhost
run(host='127.0.0.1', port=8080)

Error Prevention and Debugging

To prevent such errors, developers should:

By understanding the working principles of the getaddrinfo function and common failure causes, developers can more effectively diagnose and resolve connection issues in network programming.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.