79362215

Date: 2025-01-16 15:48:42
Score: 1.5
Natty:
Report link

I'm not able to truly disable the security certificate check, even with the two last proposed solutions.

I use the pyowm module and at some point, it calls one of its library where request is used and where it forces the verification. See below the get_json function of the HttpClient object with verify=self.config['connection']['verify_ssl_certs']

The only solution I found was to replace that line by verify = false but I would prefer to set a global variable in my original code instead of changing the code directly in one of the pyowm script.

Any ideas ? (safety warning : I'm not a developer, I just know the minimum to play with python to make my job easier)

Thanks.

class HttpClient:

"""
An HTTP client encapsulating some config data and abstarcting away data raw retrieval

:param api_key: the OWM API key
:type api_key: str
:param config: the configuration dictionary (if not provided, a default one will be used)
:type config: dict
:param root_uri: the root URI of the API endpoint
:type root_uri: str
:param admits_subdomains: if the root URI of the API endpoint admits subdomains based on the subcription type (default: True)
:type admits_subdomains: bool
"""

def __init__(self, api_key, config, root_uri, admits_subdomains=True):
    assert isinstance(api_key, str)
    self.api_key = api_key
    assert isinstance(config, dict)
    self.config = config
    assert isinstance(root_uri, str)
    self.root_uri = root_uri
    assert isinstance(admits_subdomains, bool)
    self.admits_subdomains = admits_subdomains

    if self.config['connection']['max_retries'] is not None:
        # this adapter tells how to perform retries
        self.session_adapter = HTTPAdapter(
            max_retries=Retry(
                total=self.config['connection']['max_retries'],
                status_forcelist=[429, 500, 502, 503, 504],
                method_whitelist=["HEAD", "GET", "PUT", "DELETE", "OPTIONS", "TRACE"]
            )
        )
        # this is the adapted requests client
        self.http = requests.Session()
        self.http.mount("https://", self.session_adapter)
        self.http.mount("http://", self.session_adapter)
    else:
        self.http = requests

def get_json(self, path, params=None, headers=None):
    builder = HttpRequestBuilder(self.root_uri, self.api_key, self.config, has_subdomains=self.admits_subdomains)\
        .with_path(path)\
        .with_api_key()\
        .with_language()\
        .with_query_params(params if params is not None else dict())\
        .with_headers(headers if headers is not None else dict())
    url, params, headers, proxies = builder.build()
    try:
        resp = self.http.get(url, params=params, headers=headers, proxies=proxies,
                            timeout=self.config['connection']['timeout_secs'],
                            verify=self.config['connection']['verify_ssl_certs']
                            #Added by Alex
                            #verify = False
                             )
    except requests.exceptions.SSLError as e:
        raise exceptions.InvalidSSLCertificateError(str(e))
    except requests.exceptions.ConnectionError as e:
        raise exceptions.InvalidSSLCertificateError(str(e))
    except requests.exceptions.Timeout:
        raise exceptions.TimeoutError('API call timeouted')
    HttpClient.check_status_code(resp.status_code, resp.text)
    try:
        return resp.status_code, resp.json()
    except:
        raise exceptions.ParseAPIResponseError('Impossible to parse API response data')
Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Blacklisted phrase (1): Any ideas
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: xla99