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

Commit b811eeb

Browse files
committed
fix SSL context not applied to all queries
1 parent a92d008 commit b811eeb

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

twitter/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def _handle_response_with_retry(self, req, uri, arg_data, _timeout=None):
404404
retry = self.retry
405405
while retry:
406406
try:
407-
return self._handle_response(req, uri, arg_data, _timeout)
407+
return self._handle_response(req, uri, arg_data, _timeout, self.verify_context)
408408
except TwitterHTTPError as e:
409409
if e.e.code == 429:
410410
# API rate limit reached

twitter/stream.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
from .util import PY_3_OR_HIGHER
55

6+
try:
7+
import ssl
8+
except ImportError:
9+
_HAVE_SSL = False
10+
else:
11+
_HAVE_SSL = True
12+
613
if PY_3_OR_HIGHER:
714
import urllib.request as urllib_request
815
import urllib.error as urllib_error
@@ -206,9 +213,12 @@ def __iter__(self):
206213
yield self.timeout_token
207214

208215

209-
def handle_stream_response(req, uri, arg_data, block, timeout, heartbeat_timeout):
216+
def handle_stream_response(req, uri, arg_data, block, timeout, heartbeat_timeout, verify_context=True):
210217
try:
211-
handle = urllib_request.urlopen(req,)
218+
context = None
219+
if not verify_context and _HAVE_SSL:
220+
context = ssl._create_unverified_context()
221+
handle = urllib_request.urlopen(req, context=context)
212222
except urllib_error.HTTPError as e:
213223
raise TwitterHTTPError(e, uri, 'json', arg_data)
214224
return iter(TwitterJSONIter(handle, uri, arg_data, block, timeout, heartbeat_timeout))
@@ -258,7 +268,7 @@ def __init__(self, domain="stream.twitter.com", secure=True, auth=None,
258268
uriparts = (str(api_version),)
259269

260270
class TwitterStreamCall(TwitterCall):
261-
def _handle_response(self, req, uri, arg_data, _timeout=None):
271+
def _handle_response(self, req, uri, arg_data, _timeout=None, verify_context=True):
262272
return handle_stream_response(
263273
req, uri, arg_data, block,
264274
_timeout or timeout, heartbeat_timeout)

0 commit comments

Comments
 (0)