Skip to content

Commit

Permalink
Using a ssl context object to avoid deprecation warnings in py3
Browse files Browse the repository at this point in the history
  • Loading branch information
Yomguithereal authored and boogheta committed Sep 14, 2022
1 parent 9f718d6 commit fbc7aa5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions twitter/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,17 @@ def __call__(self, **kwargs):
return self._handle_response(req, uri, arg_data, _timeout)

def _handle_response(self, req, uri, arg_data, _timeout=None):
kwargs = {'cafile': certifi.where()}
kwargs = {}
if _timeout:
kwargs['timeout'] = _timeout
try:
context = None
if not self.verify_context and _HAVE_SSL:
context = ssl._create_unverified_context()
if _HAVE_SSL:
if not self.verify_context:
context = ssl._create_unverified_context()
else:
context = ssl.create_default_context()
context.load_verify_locations(cafile=certifi.where())
kwargs['context'] = context
handle = urllib_request.urlopen(req, **kwargs)
if handle.headers['Content-Type'] in ['image/jpeg', 'image/png']:
Expand Down
10 changes: 7 additions & 3 deletions twitter/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,13 @@ def __iter__(self):
def handle_stream_response(req, uri, arg_data, block, timeout, heartbeat_timeout, verify_context=True):
try:
context = None
if not verify_context and _HAVE_SSL:
context = ssl._create_unverified_context()
handle = urllib_request.urlopen(req, context=context, cafile=certifi.where())
if _HAVE_SSL:
if not verify_context:
context = ssl._create_unverified_context()
else:
context = ssl.create_default_context()
context.load_verify_locations(cafile=certifi.where())
handle = urllib_request.urlopen(req, context=context)
except urllib_error.HTTPError as e:
raise TwitterHTTPError(e, uri, 'json', arg_data)
return iter(TwitterJSONIter(handle, uri, arg_data, block, timeout, heartbeat_timeout))
Expand Down

0 comments on commit fbc7aa5

Please sign in to comment.