1
resposta

erro executar print(response)

Quando eu executo o seguinte código:

import requests
url = 'https://guilhermeonrails.github.io/api-restaurantes/restaurantes.json'
response = requests.get(url)
print(response)

ele os seguintes erros:

(venv) C:\Users\791908\Documents\estudos\python\ambvirtual>app.py
Traceback (most recent call last):
  File "C:\Users\791908\Documents\estudos\python\ambvirtual\venv\Lib\site-packages\urllib3\connectionpool.py", line 467, in _make_request
    self._validate_conn(conn)
  File "C:\Users\791908\Documents\estudos\python\ambvirtual\venv\Lib\site-packages\urllib3\connectionpool.py", line 1099, in _validate_conn
    conn.connect()
  File "C:\Users\791908\Documents\estudos\python\ambvirtual\venv\Lib\site-packages\urllib3\connection.py", line 653, in connect
    sock_and_verified = _ssl_wrap_socket_and_match_hostname(
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\791908\Documents\estudos\python\ambvirtual\venv\Lib\site-packages\urllib3\connection.py", line 806, in _ssl_wrap_socket_and_match_hostname
    ssl_sock = ssl_wrap_socket(
               ^^^^^^^^^^^^^^^^
  File "C:\Users\791908\Documents\estudos\python\ambvirtual\venv\Lib\site-packages\urllib3\util\ssl_.py", line 465, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\791908\Documents\estudos\python\ambvirtual\venv\Lib\site-packages\urllib3\util\ssl_.py", line 509, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)    
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\791908\AppData\Local\Programs\Python\Python311\Lib\ssl.py", 
line 517, in wrap_socket
    return self.sslsocket_class._create(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\791908\AppData\Local\Programs\Python\Python311\Lib\ssl.py", 
line 1104, in _create
    self.do_handshake()
  File "C:\Users\791908\AppData\Local\Programs\Python\Python311\Lib\ssl.py", 
line 1382, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled (_ssl.c:1006)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\791908\Documents\estudos\python\ambvirtual\venv\Lib\site-packages\urllib3\connectionpool.py", line 793, in urlopen
    response = self._make_request(
               ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\791908\Documents\estudos\python\ambvirtual\venv\Lib\site-packages\urllib3\connectionpool.py", line 491, in _make_request
    raise new_e
urllib3.exceptions.SSLError: [SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled (_ssl.c:1006)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\791908\Documents\estudos\python\ambvirtual\venv\Lib\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Users\791908\Documents\estudos\python\ambvirtual\venv\Lib\site-packages\urllib3\connectionpool.py", line 847, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "C:\Users\791908\Documents\estudos\python\ambvirtual\venv\Lib\site-packages\urllib3\util\retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='guilhermeonrails.github.io', port=443): Max retries exceeded with url: /api-restaurantes/restaurantes.json (Caused by SSLError(SSLError(1, '[SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled (_ssl.c:1006)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\791908\Documents\estudos\python\ambvirtual\app.py", line 3, 
in <module>
    response = requests.get(url)
               ^^^^^^^^^^^^^^^^^
  File "C:\Users\791908\Documents\estudos\python\ambvirtual\venv\Lib\site-packages\requests\api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\791908\Documents\estudos\python\ambvirtual\venv\Lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\791908\Documents\estudos\python\ambvirtual\venv\Lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
..... tem continuacao
1 resposta

Oi, Carolina! Tudo bem?

Esse erro parece ser causado por uma incompatibilidade entre a versão do Python que você está usando. Parece que o servidor está desativando uma forma de renegociação de SSL que o Python está tentando usar.

Uma solução possível é forçar o requests a não verificar o SSL. Isso não é ideal, pois você está abrindo mão da verificação de segurança, mas pode resolver o problema imediato. Você pode fazer isso configurando verify como False no método get():

import requests
url = 'https://guilhermeonrails.github.io/api-restaurantes/restaurantes.json'
response = requests.get(url, verify=False)
print(response)

Se isso resolver o problema, considere atualizar a versão do Python.

Espero ter ajudado e boa sorte nos estudos!

Caso este post tenha lhe ajudado, por favor, marcar como solucionado ✓.