Skip to content

Commit

Permalink
fix SSL context not applied to all queries
Browse files Browse the repository at this point in the history
  • Loading branch information
boogheta committed May 31, 2021
1 parent a92d008 commit b811eeb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion twitter/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def _handle_response_with_retry(self, req, uri, arg_data, _timeout=None):
retry = self.retry
while retry:
try:
return self._handle_response(req, uri, arg_data, _timeout)
return self._handle_response(req, uri, arg_data, _timeout, self.verify_context)
except TwitterHTTPError as e:
if e.e.code == 429:
# API rate limit reached
Expand Down
16 changes: 13 additions & 3 deletions twitter/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

from .util import PY_3_OR_HIGHER

try:
import ssl
except ImportError:
_HAVE_SSL = False
else:
_HAVE_SSL = True

if PY_3_OR_HIGHER:
import urllib.request as urllib_request
import urllib.error as urllib_error
Expand Down Expand Up @@ -206,9 +213,12 @@ def __iter__(self):
yield self.timeout_token


def handle_stream_response(req, uri, arg_data, block, timeout, heartbeat_timeout):
def handle_stream_response(req, uri, arg_data, block, timeout, heartbeat_timeout, verify_context=True):
try:
handle = urllib_request.urlopen(req,)
context = None
if not verify_context and _HAVE_SSL:
context = ssl._create_unverified_context()
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 Expand Up @@ -258,7 +268,7 @@ def __init__(self, domain="stream.twitter.com", secure=True, auth=None,
uriparts = (str(api_version),)

class TwitterStreamCall(TwitterCall):
def _handle_response(self, req, uri, arg_data, _timeout=None):
def _handle_response(self, req, uri, arg_data, _timeout=None, verify_context=True):
return handle_stream_response(
req, uri, arg_data, block,
_timeout or timeout, heartbeat_timeout)
Expand Down

0 comments on commit b811eeb

Please sign in to comment.