Search code examples
pythonwebpyscript

(Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) ) pyscript


(Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) )

It just spits that out if I attempt to use requests

<py-env>
- requests
</py-env>
url = "https://call-of-duty-modern-warfare.p.rapidapi.com/warzone/" + name2 + "/acti"

headers = {
"X-RapidAPI-Host": "######",
"X-RapidAPI-Key": "######"
}

response = requests.request("GET", url, headers=headers)

data_dict = json.loads(response.text)
brstats = data_dict["br_all"]
print(brstats["kdRatio"])`

Solution

  • The requests packages uses the operating system's TCP Socket API. That API is not available inside the web browser virtual sandbox. This is a security restriction on all browser applications.

    Rewrite your code to use either pyfetch() or fetch().

    There might be a potential problem that developers run into. That is CORS. The browser enforces CORS when accessing HTTP endpoints. The endpoint that you are calling must support CORS headers on responses. Otherwise, you will get a CORS error in the browser. This is another security restriction. The only solution if there is a CORS error is to ask the owner of the endpoint to fix that problem (add CORS response headers).

    Example using pyfetch():

    from pyodide.http import pyfetch, FetchResponse
    
    response = await pyfetch(url, method="GET", headers=headers)
    
    data_dict = await response.json()
    

    If you need access to the response headers, you must use fetch() because pyfetch() does not return them. pyfetch is a wrapper for JavaScript fetch().

    Example using fetch():

    from js import fetch, Object
    from pyodide.http import to_js
    
    response = await fetch(
        url,
        method="GET",
        headers=Object.fromEntries(to_js(headers))
    
    data_dict = (await response.json()).to_py()