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

Question:

I’m trying to retrieve audio files via URLs retrieved from conllu files. I receive the link, but when I want to download the file, I get the following error, while when I enter the link on the browser I manage to download the file, however I have more than 300 audio files to collect, I can’t collect them all manually. Can you help me ?

Here is my code:

import os
import re
import urllib.request
from conll3 import *

import ssl
ssl._create_default_https_context = ssl._create_unverified_context


def extract_and_save_wav(file_path, output_directory):
    """
        Extracts and saves WAV audio file from a data file.

        Args:
             file_path (str): Path of the data file.
             output_directory (str): Output directory for the WAV audio file.

        Return:
             str or None: Path of the extracted and saved WAV audio file, or None if no audio file is found.
    """
    for root, dirs, files in os.walk(directory_path):
        for file in files:
            if file.endswith(".conllu"):
                file_path = os.path.join(root, file)
                trees = conllFile2trees(file_path)
                print(file_path)
                for tree in trees:
                    tree_str = str(tree)
                    sound_url_match = re.search(r'# sound_url = (.+)', tree_str)  # Recherche de l'URL du fichier audio
                    if sound_url_match:
                        sound_url = sound_url_match.group(1)
                        print(sound_url)
                        if sound_url.endswith('.wav'):
                            file_name = os.path.basename(file_path)
                            file_name_without_ext = os.path.splitext(file_name)[0]
                            output_file_name = f"{file_name_without_ext}.wav"
                            if output_directory is not None:
                                output_path = os.path.join(output_directory, output_file_name)
                                urllib.request.urlretrieve(sound_url, output_path)  # Téléchargement et sauvegarde du fichier audio
                                return output_path
    return None


directory_path = "../SUD_Naija-NSC-master/non_gold/"
output_wav = "../WAV/non_gold/"

extract_and_save_wav(directory_path, output_wav)

Here is the error message:

Reloaded modules: conll3
../SUD_Naija-NSC-master/non_gold/LAG_01_Hairdressing_M.conllu

Traceback (most recent call last):

  File ~/miniconda3/envs/tal-ml/lib/python3.10/urllib/request.py:1348 in do_open
    h.request(req.get_method(), req.selector, req.data, headers,

  File ~/miniconda3/envs/tal-ml/lib/python3.10/http/client.py:1283 in request
    self._send_request(method, url, body, headers, encode_chunked)

  File ~/miniconda3/envs/tal-ml/lib/python3.10/http/client.py:1329 in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)

  File ~/miniconda3/envs/tal-ml/lib/python3.10/http/client.py:1278 in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)

  File ~/miniconda3/envs/tal-ml/lib/python3.10/http/client.py:1038 in _send_output
    self.send(msg)

  File ~/miniconda3/envs/tal-ml/lib/python3.10/http/client.py:976 in send
    self.connect()

  File ~/miniconda3/envs/tal-ml/lib/python3.10/http/client.py:1455 in connect
    self.sock = self._context.wrap_socket(self.sock,

  File ~/miniconda3/envs/tal-ml/lib/python3.10/ssl.py:513 in wrap_socket
    return self.sslsocket_class._create(

  File ~/miniconda3/envs/tal-ml/lib/python3.10/ssl.py:1071 in _create
    self.do_handshake()

  File ~/miniconda3/envs/tal-ml/lib/python3.10/ssl.py:1342 in do_handshake
    self._sslobj.do_handshake()

SSLError: [SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled (_ssl.c:1007)


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File ~/miniconda3/envs/tal-ml/lib/python3.10/site-packages/spyder_kernels/py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File ~/Desktop/Stage 2023-2024/Python/recuperation_audio.py:47
    extract_and_save_wav(directory_path, output_wav)

  File ~/Desktop/Stage 2023-2024/Python/recuperation_audio.py:39 in extract_and_save_wav
    urllib.request.urlretrieve(sound_url, output_path)  # Téléchargement et sauvegarde du fichier audio

  File ~/miniconda3/envs/tal-ml/lib/python3.10/urllib/request.py:241 in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:

  File ~/miniconda3/envs/tal-ml/lib/python3.10/urllib/request.py:216 in urlopen
    return opener.open(url, data, timeout)

  File ~/miniconda3/envs/tal-ml/lib/python3.10/urllib/request.py:525 in open
    response = meth(req, response)

  File ~/miniconda3/envs/tal-ml/lib/python3.10/urllib/request.py:634 in http_response
    response = self.parent.error(

  File ~/miniconda3/envs/tal-ml/lib/python3.10/urllib/request.py:557 in error
    result = self._call_chain(*args)

  File ~/miniconda3/envs/tal-ml/lib/python3.10/urllib/request.py:496 in _call_chain
    result = func(*args)

  File ~/miniconda3/envs/tal-ml/lib/python3.10/urllib/request.py:749 in http_error_302
    return self.parent.open(new, timeout=req.timeout)

  File ~/miniconda3/envs/tal-ml/lib/python3.10/urllib/request.py:519 in open
    response = self._open(req, data)

  File ~/miniconda3/envs/tal-ml/lib/python3.10/urllib/request.py:536 in _open
    result = self._call_chain(self.handle_open, protocol, protocol +

  File ~/miniconda3/envs/tal-ml/lib/python3.10/urllib/request.py:496 in _call_chain
    result = func(*args)

  File ~/miniconda3/envs/tal-ml/lib/python3.10/urllib/request.py:1391 in https_open
    return self.do_open(http.client.HTTPSConnection, req,

  File ~/miniconda3/envs/tal-ml/lib/python3.10/urllib/request.py:1351 in do_open
    raise URLError(err)

URLError: <urlopen error [SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled (_ssl.c:1007)>
Asked By: Peilin

||

Answers:

Here is an example how you can twak the https:// adapter to accept connections like these:

import ssl
import warnings

import requests
import requests.packages.urllib3.exceptions as urllib3_exceptions

warnings.simplefilter("ignore", urllib3_exceptions.InsecureRequestWarning)


class TLSAdapter(requests.adapters.HTTPAdapter):
    def init_poolmanager(self, *args, **kwargs):
        ctx = ssl.create_default_context()
        ctx.check_hostname = False
        ctx.set_ciphers("DEFAULT@SECLEVEL=1")
        ctx.options |= 0x4
        kwargs["ssl_context"] = ctx
        return super(TLSAdapter, self).init_poolmanager(*args, **kwargs)


url = "http://naijasyncor.huma-num.fr/carte/mp3/LAG_01_Hairdressing_M.wav"

with requests.session() as s:
    s.mount("https://", TLSAdapter())

    response = s.get(url)
    with open("LAG_01_Hairdressing_M.wav", "wb") as f_out:
        f_out.write(response.content)

This downloads LAG_01_Hairdressing_M.wav:

$ ls -alF *wav
-rw-r--r-- 1 root root 14044900 oct 30 22:17 LAG_01_Hairdressing_M.wav
Answered By: Andrej Kesely
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.