Python - http.client : GET request to TMDB API failed Error

717 views Asked by At

I am badly stuck with the error and a submission due.

        conn = http.client.HTTPSConnection("api.themoviedb.org/3")
        conn.request('GET', '/person/5064/movie_credits?api_key={api_key}&language=en-US')                                         
        r1 = conn.getresponse()
        print(r1.status, r1.reason)
        data1 = r1.read()
        print(type(data1))
        print(data1)        

I have tried accessing the api with my api key from the browser and it works. On Running the code on the laptop I get the following error:

C:\Users\Dhaval Desai\Desktop\Gatech\DVA\hw1-skeleton>"C:/Users/Dhaval Desai/AppData/Local/Programs/Python/Python37/python.exe" "c:/Users/Dhaval Desai/Desktop/Gatech/DVA/hw1-skeleton/Q1/submission.py"
Traceback (most recent call last):
  File "c:/Users/Dhaval Desai/Desktop/Gatech/DVA/hw1-skeleton/Q1/submission.py", line 311, in <module>
    tmdb_api_utils.get_movie_credits_for_person("5064")
  File "c:/Users/Dhaval Desai/Desktop/Gatech/DVA/hw1-skeleton/Q1/submission.py", line 214, in get_movie_credits_for_person
    conn.request("GET", "/movie/550?api_key=8dcea7325ee7c9571a21d3184cffdf34&language=en-US")
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1252, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1298, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1247, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 966, in send
    self.connect()
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1414, in connect
    super().connect()
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 938, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
1

There are 1 answers

0
DisappointedByUnaccountableMod On BEST ANSWER

The hostname you're trying to access is "api.themoviedb.org/3" which, err, isn't a hostname.

As the error traceback shows, it's the second line where the error occurs - presumably http.client.HTTPSConnection() doesn't actually look up the hostname you provided until then.

For example trying to resolve your hostname gives the exact same error (although with different line numbers, I'm using Python 3.8.3 on Windows x64):

>>> import socket
>>> socket.getaddrinfo("api.themoviedb.org/3",443)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python38\lib\socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

Just remove the /3?