Error: urllib.error.URLError:SSL

For a few days now, my app ( https://mapagasolineras.streamlit.app/ ) has stopped working in the cloud, although it continues to work locally without any issues.
The error occurs when reading an xls file with this command:
URL = “https://geoportalgasolineras.es/resources/files/preciosEESS_es.xls
df = pd.read_excel(URL, skiprows=3, engine=“xlrd”)

Can anyone help me?
Thank you very much.

In the past, this code solved a ssl error:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

Debug:
urllib.error.URLError: <urlopen error [SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled (_ssl.c:1129)>

urllib.error.URLError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you’re on Streamlit Cloud, click on ‘Manage app’ in the lower right of your app).
Traceback:
File “/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 565, in _run_script
exec(code, module.dict)
File “/app/mapapreciocarburantes/MapaPrecioCarburantes.py”, line 127, in
df, prov_data, gdf, FAct = cargarFichero()
File “/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/caching/cache_utils.py”, line 186, in call
return self._get_or_create_cached_value(args, kwargs)
File “/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/caching/cache_utils.py”, line 211, in _get_or_create_cached_value
return self._handle_cache_miss(cache, value_key, func_args, func_kwargs)
File “/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/caching/cache_utils.py”, line 265, in _handle_cache_miss
computed_value = self._info.func(*func_args, **func_kwargs)
File “/app/mapapreciocarburantes/MapaPrecioCarburantes.py”, line 49, in cargarFichero
df = pd.read_excel(URL, skiprows=3, engine=“xlrd”)
File “/home/appuser/venv/lib/python3.9/site-packages/pandas/io/excel/_base.py”, line 478, in read_excel
io = ExcelFile(io, storage_options=storage_options, engine=engine)
File “/home/appuser/venv/lib/python3.9/site-packages/pandas/io/excel/_base.py”, line 1513, in init
self._reader = self._engines[engine](self._io, storage_options=storage_options)
File “/home/appuser/venv/lib/python3.9/site-packages/pandas/io/excel/_xlrd.py”, line 35, in init
super().init(filepath_or_buffer, storage_options=storage_options)
File “/home/appuser/venv/lib/python3.9/site-packages/pandas/io/excel/_base.py”, line 530, in init
self.handles = get_handle(
File “/home/appuser/venv/lib/python3.9/site-packages/pandas/io/common.py”, line 716, in get_handle
ioargs = _get_filepath_or_buffer(
File “/home/appuser/venv/lib/python3.9/site-packages/pandas/io/common.py”, line 368, in _get_filepath_or_buffer
with urlopen(req_info) as req:
File “/home/appuser/venv/lib/python3.9/site-packages/pandas/io/common.py”, line 270, in urlopen
return urllib.request.urlopen(*args, **kwargs)
File “/usr/local/lib/python3.9/urllib/request.py”, line 214, in urlopen
return opener.open(url, data, timeout)
File “/usr/local/lib/python3.9/urllib/request.py”, line 517, in open
response = self._open(req, data)
File “/usr/local/lib/python3.9/urllib/request.py”, line 534, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File “/usr/local/lib/python3.9/urllib/request.py”, line 494, in _call_chain
result = func(*args)
File “/usr/local/lib/python3.9/urllib/request.py”, line 1389, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File “/usr/local/lib/python3.9/urllib/request.py”, line 1349, in do_open
raise URLError(err)

I was able to get it by creating a custom requests session (using this method python - SSL error unsafe legacy renegotiation disabled - Stack Overflow), getting the url with that, and then passing the content into pandas.

import streamlit as st
import ssl
import urllib3
import requests
import pandas as pd
import io


class CustomHttpAdapter(requests.adapters.HTTPAdapter):
    # "Transport adapter" that allows us to use custom ssl_context.

    def __init__(self, ssl_context=None, **kwargs):
        self.ssl_context = ssl_context
        super().__init__(**kwargs)

    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = urllib3.poolmanager.PoolManager(
            num_pools=connections,
            maxsize=maxsize,
            block=block,
            ssl_context=self.ssl_context,
        )


def get_legacy_session():
    ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
    ctx.options |= 0x4  # OP_LEGACY_SERVER_CONNECT
    session = requests.session()
    session.mount("https://", CustomHttpAdapter(ctx))
    return session


URL = "https://geoportalgasolineras.es/resources/files/preciosEESS_es.xls"
res = get_legacy_session().get(URL)
df = pd.read_excel(io.BytesIO(res.content), skiprows=3, engine="xlrd")

st.write(df)
1 Like

Hi,
This code solves the error, now my app works correctly. Thank you very much.
Best regards.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.