You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python使用HTTPClient发GET请求遇socket.gaierror错误求助

问题描述

我是Python新手,在使用http.client发送GET请求时遇到socket.gaierror: [Errno 11001] getaddrinfo failed错误。

我的代码如下:

payload = ''
headers = {
    'Authorization': 'Bearer [accf90.................e]',
    'Accept-Encoding': 'gzip, deflate'
    }
conn = http.client.HTTPSConnection("[https://search-api.hyperguest.io]")
conn.request("GET", "/2.0/?nights=%(nights)s&guests=2&hotelIds=%(hotel_code)s&checkIn=2023-11-26" % {
        "hotel_code":hotel_code,
        "nights":nights
    }, payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

报错信息:

File "C:\Users\iRFAN\Desktop\lexstays\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\Users\iRFAN\Desktop\lexstays\venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\iRFAN\Desktop\lexstays\lex-stay-web-py\lex_stays\lex_stays_user\views.py", line 861, in check_availability
    conn.request("GET", "/2.0/?nights=%(nights)s&guests=2&hotelIds=%(hotel_code)s&checkIn=2023-11-26" % {
  File "C:\Users\iRFAN\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1279, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\iRFAN\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1325, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\iRFAN\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1274, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\iRFAN\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1034, in _send_output
    self.send(msg)
  File "C:\Users\iRFAN\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 974, in send
    self.connect()
  File "C:\Users\iRFAN\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1441, in connect
    super().connect()
  File "C:\Users\iRFAN\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 945, in connect
    self.sock = self._create_connection(
  File "C:\Users\iRFAN\AppData\Local\Programs\Python\Python39\lib\socket.py", line 823, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\iRFAN\AppData\Local\Programs\Python\Python39\lib\socket.py", line 954, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
[26/Nov/2023 19:02:16] "GET /check-availability?check_out=28%2F11%2F23&check_in=27%2F11%2F23&room_slug=two-rooms-bungalow HTTP/1.1" 500 118766
解决方案

这个错误是域名解析失败导致的,核心问题出在HTTPSConnection的参数上:

  • 你传入的是带方括号和https://前缀的字符串"[https://search-api.hyperguest.io]",但HTTPSConnection只需要传入纯域名,不需要协议前缀和多余的方括号,否则系统无法正确解析域名。

修改后的正确代码:

payload = ''
headers = {
    'Authorization': 'Bearer [accf90.................e]',
    'Accept-Encoding': 'gzip, deflate'
    }
# 移除方括号和https://前缀,仅保留域名
conn = http.client.HTTPSConnection("search-api.hyperguest.io")
conn.request("GET", "/2.0/?nights=%(nights)s&guests=2&hotelIds=%(hotel_code)s&checkIn=2023-11-26" % {
        "hotel_code":hotel_code,
        "nights":nights
    }, payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

额外建议:
对于Python HTTP请求,更推荐使用requests库,代码更简洁易读,还能自动处理很多细节问题。上述请求用requests实现的代码如下:

import requests

url = "https://search-api.hyperguest.io/2.0/"
params = {
    "nights": nights,
    "guests": 2,
    "hotelIds": hotel_code,
    "checkIn": "2023-11-26"
}
headers = {
    'Authorization': 'Bearer [accf90.................e]',
    'Accept-Encoding': 'gzip, deflate'
}

response = requests.get(url, params=params, headers=headers)
print(response.text)

内容的提问来源于stack exchange,提问作者Hiba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 13:09:54