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

Commit bf33714

Browse files
guangtaopiboogheta
authored andcommitted
url lib support no auth for https
1 parent 3f58381 commit bf33714

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

twitter/api.py

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

44
from .util import PY_3_OR_HIGHER, actually_bytes
55

6+
try:
7+
import ssl
8+
except ImportError:
9+
_have_ssl = False
10+
else:
11+
_have_ssl = True
12+
613
try:
714
import urllib.request as urllib_request
815
import urllib.error as urllib_error
@@ -51,6 +58,7 @@ class TwitterHTTPError(TwitterError):
5158
Exception thrown by the Twitter object when there is an
5259
HTTP error interacting with twitter.com.
5360
"""
61+
5462
def __init__(self, e, uri, format, uriparts):
5563
self.e = e
5664
self.uri = uri
@@ -82,10 +90,10 @@ def __init__(self, e, uri, format, uriparts):
8290
def __str__(self):
8391
fmt = ("." + self.format) if self.format else ""
8492
return (
85-
"Twitter sent status %i for URL: %s%s using parameters: "
86-
"(%s)\ndetails: %s" % (
87-
self.e.code, self.uri, fmt, self.uriparts,
88-
self.response_data))
93+
"Twitter sent status %i for URL: %s%s using parameters: "
94+
"(%s)\ndetails: %s" % (
95+
self.e.code, self.uri, fmt, self.uriparts,
96+
self.response_data))
8997

9098

9199
class TwitterResponse(object):
@@ -144,6 +152,7 @@ def wrap_response(response, headers):
144152

145153
POST_ACTIONS_RE = re.compile('(' + '|'.join(POST_ACTIONS) + r')(/\d+)?$')
146154

155+
147156
def method_for_uri(uri):
148157
if POST_ACTIONS_RE.search(uri):
149158
return "POST"
@@ -175,12 +184,11 @@ def build_uri(orig_uriparts, kwargs):
175184

176185

177186
class TwitterCall(object):
178-
179187
TWITTER_UNAVAILABLE_WAIT = 30 # delay after HTTP codes 502, 503 or 504
180188

181189
def __init__(
182190
self, auth, format, domain, callable_cls, uri="",
183-
uriparts=None, secure=True, timeout=None, gzip=False, retry=False):
191+
uriparts=None, secure=True, timeout=None, gzip=False, retry=False, verify_context=False):
184192
self.auth = auth
185193
self.format = format
186194
self.domain = domain
@@ -191,6 +199,7 @@ def __init__(
191199
self.timeout = timeout
192200
self.gzip = gzip
193201
self.retry = retry
202+
self.verify_context = verify_context
194203

195204
def __getattr__(self, k):
196205

@@ -208,7 +217,7 @@ def extend_call(arg):
208217
auth=self.auth, format=self.format, domain=self.domain,
209218
callable_cls=self.callable_cls, timeout=self.timeout,
210219
secure=self.secure, gzip=self.gzip, retry=self.retry,
211-
uriparts=self.uriparts + (arg,))
220+
uriparts=self.uriparts + (arg,), verify_context=self.verify_context)
212221

213222
if k == "_":
214223
return extend_call
@@ -350,16 +359,19 @@ def __call__(self, **kwargs):
350359
req.get_method = lambda: method
351360

352361
if self.retry:
353-
return self._handle_response_with_retry(req, uri, arg_data, _timeout)
362+
return self._handle_response_with_retry(req, uri, arg_data, _timeout, self.verify_context)
354363
else:
355-
return self._handle_response(req, uri, arg_data, _timeout)
364+
return self._handle_response(req, uri, arg_data, _timeout, self.verify_context)
356365

357-
def _handle_response(self, req, uri, arg_data, _timeout=None):
366+
def _handle_response(self, req, uri, arg_data, _timeout=None, verify_context=False):
358367
kwargs = {}
359368
if _timeout:
360369
kwargs['timeout'] = _timeout
361370
try:
362-
handle = urllib_request.urlopen(req, **kwargs)
371+
context = None
372+
if not verify_context:
373+
context = ssl._create_unverified_context()
374+
handle = urllib_request.urlopen(req, **kwargs, context=context)
363375
if handle.headers['Content-Type'] in ['image/jpeg', 'image/png']:
364376
return handle
365377
try:
@@ -536,10 +548,11 @@ class Twitter(TwitterCall):
536548
of XML.
537549
538550
"""
551+
539552
def __init__(
540553
self, format="json",
541554
domain="api.twitter.com", secure=True, auth=None,
542-
api_version=_DEFAULT, retry=False):
555+
api_version=_DEFAULT, retry=False, verify_context=False):
543556
"""
544557
Create a new twitter API connector.
545558
@@ -581,7 +594,7 @@ def __init__(
581594
TwitterCall.__init__(
582595
self, auth=auth, format=format, domain=domain,
583596
callable_cls=TwitterCall,
584-
secure=secure, uriparts=uriparts, retry=retry)
597+
secure=secure, uriparts=uriparts, retry=retry, verify_context=verify_context)
585598

586599

587600
__all__ = ["Twitter", "TwitterError", "TwitterHTTPError", "TwitterResponse"]

0 commit comments

Comments
 (0)