From 4d8edcf9adc6cf9b09e94d4590b350ad2bf4fcee Mon Sep 17 00:00:00 2001 From: Yomguithereal Date: Wed, 14 Sep 2022 11:19:26 +0200 Subject: [PATCH] Using certifi to find ssl cert for nontrivial python installations --- .github/workflows/deploy.yml | 1 + .github/workflows/tests.yml | 1 + requirements.txt | 1 + setup.py | 1 + twitter/api.py | 4 +++- twitter/stream.py | 7 +++++-- 6 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 requirements.txt diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 5e68596..8dad013 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -29,6 +29,7 @@ jobs: run: | python -m pip install -U pip python -m pip install -U setuptools twine wheel + python -m pip install -r requirements.txt - name: Build package run: | diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7e1e07d..1d22399 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -38,6 +38,7 @@ jobs: run: | python -m pip install -U pip python -m pip install -U pytest pytest-cov mock + python -m pip install -r requirements.txt - name: Tests shell: bash diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..963eac5 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +certifi diff --git a/setup.py b/setup.py index 474ebe8..930ffe4 100644 --- a/setup.py +++ b/setup.py @@ -47,6 +47,7 @@ def local_scheme(version): zip_safe=True, use_scm_version={"local_scheme": local_scheme}, setup_requires=["setuptools_scm"], + install_requires=["certifi"], entry_points=""" # -*- Entry points: -*- [console_scripts] diff --git a/twitter/api.py b/twitter/api.py index 3389c8e..0eed604 100644 --- a/twitter/api.py +++ b/twitter/api.py @@ -17,6 +17,8 @@ import urllib2 as urllib_request import urllib2 as urllib_error +import certifi + try: from cStringIO import StringIO except ImportError: @@ -369,7 +371,7 @@ def __call__(self, **kwargs): return self._handle_response(req, uri, arg_data, _timeout) def _handle_response(self, req, uri, arg_data, _timeout=None): - kwargs = {} + kwargs = {'cafile': certifi.where()} if _timeout: kwargs['timeout'] = _timeout try: diff --git a/twitter/stream.py b/twitter/stream.py index 4004dd7..04efa98 100644 --- a/twitter/stream.py +++ b/twitter/stream.py @@ -16,11 +16,14 @@ else: import urllib2 as urllib_request import urllib2 as urllib_error + +import certifi + import json from ssl import SSLError import socket import codecs -import sys, select, time +import select, time from .api import TwitterCall, wrap_response, TwitterHTTPError @@ -218,7 +221,7 @@ def handle_stream_response(req, uri, arg_data, block, timeout, heartbeat_timeout context = None if not verify_context and _HAVE_SSL: context = ssl._create_unverified_context() - handle = urllib_request.urlopen(req, context=context) + handle = urllib_request.urlopen(req, context=context, cafile=certifi.where()) except urllib_error.HTTPError as e: raise TwitterHTTPError(e, uri, 'json', arg_data) return iter(TwitterJSONIter(handle, uri, arg_data, block, timeout, heartbeat_timeout))