Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit fbc7aa5

Browse files
Yomguitherealboogheta
authored andcommitted
Using a ssl context object to avoid deprecation warnings in py3
1 parent 9f718d6 commit fbc7aa5

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

twitter/api.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,17 @@ def __call__(self, **kwargs):
371371
return self._handle_response(req, uri, arg_data, _timeout)
372372

373373
def _handle_response(self, req, uri, arg_data, _timeout=None):
374-
kwargs = {'cafile': certifi.where()}
374+
kwargs = {}
375375
if _timeout:
376376
kwargs['timeout'] = _timeout
377377
try:
378378
context = None
379-
if not self.verify_context and _HAVE_SSL:
380-
context = ssl._create_unverified_context()
379+
if _HAVE_SSL:
380+
if not self.verify_context:
381+
context = ssl._create_unverified_context()
382+
else:
383+
context = ssl.create_default_context()
384+
context.load_verify_locations(cafile=certifi.where())
381385
kwargs['context'] = context
382386
handle = urllib_request.urlopen(req, **kwargs)
383387
if handle.headers['Content-Type'] in ['image/jpeg', 'image/png']:

twitter/stream.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,13 @@ def __iter__(self):
219219
def handle_stream_response(req, uri, arg_data, block, timeout, heartbeat_timeout, verify_context=True):
220220
try:
221221
context = None
222-
if not verify_context and _HAVE_SSL:
223-
context = ssl._create_unverified_context()
224-
handle = urllib_request.urlopen(req, context=context, cafile=certifi.where())
222+
if _HAVE_SSL:
223+
if not verify_context:
224+
context = ssl._create_unverified_context()
225+
else:
226+
context = ssl.create_default_context()
227+
context.load_verify_locations(cafile=certifi.where())
228+
handle = urllib_request.urlopen(req, context=context)
225229
except urllib_error.HTTPError as e:
226230
raise TwitterHTTPError(e, uri, 'json', arg_data)
227231
return iter(TwitterJSONIter(handle, uri, arg_data, block, timeout, heartbeat_timeout))

0 commit comments

Comments
 (0)