socket.gaierror: [Errno 11001] 使用 HTTP 客户端 python 时 getaddrinfo 失败

问题描述 投票:0回答:1

我是Python新手,使用http客户端发送GET请求。我的代码是:

    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
python http httpclient
1个回答
0
投票

从文档中,HTTPSConnection 采用主机名,而不是 URL。即使 if 是一个 URL,它也不会是方括号中的 URL。只需使用主机名:

conn = http.client.HTTPSConnection("search-api.hyperguest.io")
© www.soinside.com 2019 - 2024. All rights reserved.