Home > Software design >  (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.&q
(Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.&q

Time:06-12

I am trying to execute the below code, It uses requests to fetch data from github repository when I run on my local with parameter verify=False it runs properly but when i run this in browser it gives ssl Error, I am using python3.10 version code tries to fetch csv files from github repositories and do some manipulation with it, It uses requests.get method but when I run the code in browser i end up wit the ssl error even after setting verify=False

#The below code tries to fetch csv files from github repositories and do some manipulation with it,
It uses requests.get method but when I run the code in browser i end up wit the ssl error even after setting verify=False.#


<py-env>
   - matplotlib
   - pandas
   - seaborn
   - requests
   - lxml
</py-env>


<py-script>
import pandas as pd
import matplotlib.pyplot as plt
import glob
import os
import sys
import seaborn as sns
from js import document
from pyodide import create_proxy
from pyodide.http import open_url
from lxml import html
import requests


# Get the data#
def selectChange(event):
    
    page = requests.get('https://github.com/Nitink2001/EnergyUpgraded.git',verify=False)
    webpage = html.fromstring(page.content)
    path = webpage.xpath('//a/@href')
    li = []
    for p in path:
        if p.endswith('.csv'):
         url = 'https://github.com' p.replace('/blob/', '/raw/')
         df = pd.read_csv(url)
         li.append(df.T)
    frame = pd.concat(li)
    frame.dropna(inplace=True)
    print(frame.head())
    choice = document.getElementById("hdem").value
    console.log(choice)
    
def setup():
   # Create a JsProxy for the callback function#
    change_proxy = create_proxy(selectChange)
    e = document.getElementById("hdem")
    e.addEventListener("change", change_proxy)
setup()
</py-script>


When i run this code I encounter with the below error
pyodide.asm.js:14 
        
      # Python exception:
Traceback (most recent call last):
  File "/lib/python3.10/site-packages/urllib3/connectionpool.py", line 692, in urlopen
    conn = self._get_conn(timeout=pool_timeout)
  File "/lib/python3.10/site-packages/urllib3/connectionpool.py", line 281, in _get_conn
    return conn or self._new_conn()
  File "/lib/python3.10/site-packages/urllib3/connectionpool.py", line 1009, in _new_conn
    raise SSLError(
urllib3.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available.

#I have tried almost all the method available on stackoverflows still I am unable to fix the error.#

CodePudding user response:

Your problem is caused by importing the requests package.

The Requests package uses the operating system TCP Socket API. That API is not available in web browsers. That limitation is a security restriction imposed on all browser-based applications.

Python packages that import the requests package are not supported in PyScript.

Modify your code using one of the following methods.

from pyodide.http import open_url

page = open_url('https://github.com/Nitink2001/EnergyUpgraded.git')

Note: There is a usage restriction for open_url(). That API can only return text based data and does not support binary.

PyScript has the function pyfetch():

import asyncio
from pyodide.http import pyfetch

# this code must be in an "async" function
    
async def myfunc():
    response = await pyfetch('https://github.com/Nitink2001/EnergyUpgraded.git')
    data = await response.text()

PyScript also supports the browser Fetch API, which is the API I recommend using:

import asyncio
from js import fetch

# this code must be in an "async" function

async def myfunc():
    response = await fetch('https://github.com/Nitink2001/EnergyUpgraded.git')
    data = await response.text()
  • Related