I’m encountering an SSLError with the message “EOF occurred in violation of protocol” when attempting to make an HTTPS request using the requests library in my Python script. This error seems to be related to SSL/TLS issues during the handshake process. I’ve included the traceback below for reference. Could anyone provide insights on why this error occurs and how to resolve it?
This is my Python Code:
import requests
MY_LAT = 27.167721
MY_LONG = 78.035889
parameters = {
"lat": MY_LAT,
"lng": MY_LONG
}
response = requests.get("https://api.sunrise-sunset.org/json", params=parameters)
response.raise_for_status()
data = response.json()
print(data)
This is the error I’m getting:
Traceback (most recent call last):
File "D:pythonvenvLibsite-packagesurllib3connectionpool.py", line 467, in _make_request
self._validate_conn(conn)
File "D:pythonvenvLibsite-packagesurllib3connectionpool.py", line 1099, in _validate_conn
conn.connect()
File "D:pythonvenvLibsite-packagesurllib3connection.py", line 653, in connect
sock_and_verified = _ssl_wrap_socket_and_match_hostname(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:pythonvenvLibsite-packagesurllib3connection.py", line 806, in _ssl_wrap_socket_and_match_hostname
ssl_sock = ssl_wrap_socket(
^^^^^^^^^^^^^^^^
File "D:pythonvenvLibsite-packagesurllib3utilssl_.py", line 465, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:pythonvenvLibsite-packagesurllib3utilssl_.py", line 509, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:Program FilesPython312Libssl.py", line 455, in wrap_socket
return self.sslsocket_class._create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:Program FilesPython312Libssl.py", line 1042, in _create
self.do_handshake()
File "C:Program FilesPython312Libssl.py", line 1320, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: [SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1000)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:pythonvenvLibsite-packagesurllib3connectionpool.py", line 793, in urlopen
response = self._make_request(
^^^^^^^^^^^^^^^^^^^
File "D:pythonvenvLibsite-packagesurllib3connectionpool.py", line 491, in _make_request
raise new_e
urllib3.exceptions.SSLError: [SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1000)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:pythonvenvLibsite-packagesrequestsadapters.py", line 486, in send
resp = conn.urlopen(
^^^^^^^^^^^^^
File "D:pythonvenvLibsite-packagesurllib3connectionpool.py", line 847, in urlopen
retries = retries.increment(
^^^^^^^^^^^^^^^^^^
File "D:pythonvenvLibsite-packagesurllib3utilretry.py", line 515, in increment
raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.sunrise-sunset.org', port=443):
Max retries exceeded with url: /json?lat=27.167721&lng=78.035889 (Caused by SSLError(SSLEOFError(8, '[SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1000)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:pythonmain.py", line 11, in <module>
response = requests.get("https://api.sunrise-sunset.org/json", params=parameters)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:pythonvenvLibsite-packagesrequestsapi.py", line 73, in get
return request("get", url, params=params, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:pythonvenvLibsite-packagesrequestsapi.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:pythonvenvLibsite-packagesrequestssessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:pythonvenvLibsite-packagesrequestssessions.py", line 703, in send
r = adapter.send(request, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:pythonvenvLibsite-packagesrequestsadapters.py", line 517, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='api.sunrise-sunset.org', port=443): Max
retries exceeded with url: /json?lat=27.167721&lng=78.035889 (Caused by SSLError(SSLEOFError(8,
'[SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1000)')))
New contributor
Byte Cheetah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.