Merge branch 'feature/cookies' into develop
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 09740c3, 98c8a78 · Browse files · 3 files·+69 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+69 added−22 removed
- other:+69 added−22 removed
Provenance
- Commit
8ff8e02- Tree
baf85af- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
09740c3- Parent 2
98c8a78
3 files changed+69 added−22 removed
modified:requests/core.py+53 added-15 removed
11 unmodified lines12 13 import urllib14 import urllib215 16+17+try:18+ import eventlet19+ eventlet.monkey_patch()20+except ImportError:21+ pass22+23+if not 'eventlet' in locals():24+ try:25+ from gevent import monkey26+ monkey.patch_all()27+ except ImportError:28+ pass29+30+31 from .packages.poster.encode import multipart_encode32 from .packages.poster.streaminghttp import register_openers33 34 38 unmodified lines73 self.method = None74 self.params = {}75 self.data = {}76 self.response = Response()77+ 78 self.auth = None79+ self.cookiejar = None80 self.sent = False81 82 83 def __repr__(self):8 unmodified lines92 object.__setattr__(self, name, value)93 94 95 def _checks(self):79- """Deterministic checks for consistiency."""96+ """Deterministic checks for consistency."""97 98 if not self.url:99 raise URLRequired100 101 102 def _get_opener(self):103 """Creates appropriate opener object for urllib2."""87- 88- if self.auth:104 90- # create a password manager91- authr = urllib2.HTTPPasswordMgrWithDefaultRealm()105+ _handlers = []106 93- authr.add_password(None, self.url, self.auth.username, self.auth.password)94- handler = urllib2.HTTPBasicAuthHandler(authr)95- opener = urllib2.build_opener(handler)107+ if self.auth or self.cookiejar:108 97- # use the opener to fetch a URL109+ if self.auth:110+111+ authr = urllib2.HTTPPasswordMgrWithDefaultRealm()112+113+ authr.add_password(None, self.url, self.auth.username, self.auth.password)114+ auth_handler = urllib2.HTTPBasicAuthHandler(authr)115+116+ _handlers.append(auth_handler)117+118+ if self.cookiejar:119+120+ cookie_handler = urllib2.HTTPCookieProcessor(cookiejar)121+ _handlers.append(cookie_handler)122+123+ opener = urllib2.build_opener(*_handlers)124 return opener.open125+126 else:127 return urllib2.urlopen128 129 4 unmodified lines134 self.response.headers = resp.info().dict135 self.response.content = resp.read()136 self.response.url = resp.url137 138+ 139 def send(self, anyway=False):140 """Sends the request. Returns True of successful, false if not.141 If there was an HTTPError during transmission,142 self.response.status_code will contain the HTTPError code.139 unmodified lines282 self.password = password283 284 285 258-def get(url, params={}, headers={}, auth=None):286+def get(url, params={}, headers={}, cookies=None, auth=None):287 """Sends a GET request. Returns :class:`Response` object.288 289 :param url: URL for the new :class:`Request` object.290 :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`.263- :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.291+ :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.292+ :param cookies: (optional) CookieJar object to send with the :class:`Request`.293 :param auth: (optional) AuthObject to enable Basic HTTP Auth.294 """295 296 r = Request()1 unmodified line298 r.method = 'GET'299 r.url = url300 r.params = params301 r.headers = headers302+ r.cookiejar = cookies303 r.auth = _detect_auth(url, auth)304 305 r.send()306 307 return r.response308 309 280-def head(url, params={}, headers={}, auth=None):310+def head(url, params={}, headers={}, cookies=None, auth=None):311 """Sends a HEAD request. Returns :class:`Response` object.312 313 :param url: URL for the new :class:`Request` object.314 :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`.315 :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.316+ :param cookies: (optional) CookieJar object to send with the :class:`Request`.317 :param auth: (optional) AuthObject to enable Basic HTTP Auth.318 """319 320 r = Request()2 unmodified lines323 r.url = url324 # return response object325 r.params = params326 r.headers = headers327+ r.cookiejar = cookies328 r.auth = _detect_auth(url, auth)329 330 r.send()331 332 return r.response333 334 303-def post(url, data={}, headers={}, files=None, auth=None):335+def post(url, data={}, headers={}, files=None, cookies=None, auth=None):336 """Sends a POST request. Returns :class:`Response` object.337 338 :param url: URL for the new :class:`Request` object.339 :param data: (optional) Dictionary of POST Data to send with the :class:`Request`.340 :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.341 :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.342+ :param cookies: (optional) CookieJar object to send with the :class:`Request`.343 :param auth: (optional) AuthObject to enable Basic HTTP Auth.344 """345 346 r = Request()5 unmodified lines352 if files:353 r.files = files354 355 r.headers = headers356+ r.cookiejar = cookies357 r.auth = _detect_auth(url, auth)358 359 r.send()360 361 return r.response362 363 330-def put(url, data='', headers={}, files={}, auth=None):364+def put(url, data='', headers={}, files={}, cookies=None, auth=None):365 """Sends a PUT request. Returns :class:`Response` object.366 367 :param url: URL for the new :class:`Request` object.368 :param data: (optional) Bytes of PUT Data to send with the :class:`Request`.369 :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.370 :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.371+ :param cookies: (optional) CookieJar object to send with the :class:`Request`.372 :param auth: (optional) AuthObject to enable Basic HTTP Auth.373 """374 375 r = Request()2 unmodified lines378 r.method = 'PUT'379 r.data = data380 r.files = files381 r.headers = headers382+ r.cookiejar = cookies383 r.auth = _detect_auth(url, auth)384 385 r.send()386 387 return r.response388 389 354-def delete(url, params={}, headers={}, auth=None):390+def delete(url, params={}, headers={}, cookies=None, auth=None):391 """Sends a DELETE request. Returns :class:`Response` object.392 393 :param url: URL for the new :class:`Request` object.394 :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`.395 :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.396+ :param cookies: (optional) CookieJar object to send with the :class:`Request`.397 :param auth: (optional) AuthObject to enable Basic HTTP Auth.398 """399 400 r = Request()1 unmodified line402 r.url = url403 r.method = 'DELETE'404 405 r.headers = headers406+ r.cookiejar = cookies407 r.auth = _detect_auth(url, auth)408 409 r.send()410 53 unmodified linesmodified:HISTORY.rst+8 added
1 History2 -------3 4+0.2.2 (2011-02-14)5+++++++++++++++++++6+* Still handles request in the event of an HTTPError. (Issue #2)7+* Eventlet and Gevent Monkeypatch support.8+* Cookie Support (Issue #1)9+10+11+12 0.2.1 (2011-02-14)13 ++++++++++++++++++14 15 * Added file attribute to POST and PUT requests for multipart-encode file uploads.15 unmodified linesmodified:README.rst+8 added-7 removed
54 unmodified lines55 56 **Requests:**57 58 All request functions return a Response object (see below).59+60+If a {filename: fileobject} dictionary is passed in (files=...), a multipart_encode upload will be performed.61+If CookieJar object is is passed in (cookies=...), the cookies will be sent with the request.62 63 GET Requests61- >>> request.get(url, params={}, headers={} auth=None)64+ >>> request.get(url, params={}, headers={}, cookies=None, auth=None)65 <request object>66 67 HEAD Requests65- >>> request.head(url, params={}, headers={} auth=None)68+ >>> request.head(url, params={}, headers={}, cookies=None, auth=None)69 <request object>70 71 PUT Requests69- >>> request.put(url, data='', headers={}, files={}, auth=None)72+ >>> request.put(url, data='', headers={}, files={}, cookies=None, auth=None)73 <request object>71- # If files dictionary ( {filename: fileobject, ...} ) is given, a multi-part upload is performed.74 75 POST Requests74- >>> request.post(url, data={}, headers={}, files={}, auth=None)76+ >>> request.post(url, data={}, headers={}, files={}, cookies=None, auth=None)77 <request object>76- # If files dictionary ( {filename: fileobject, ...} ) is given, a multi-part upload is performed.78 79 DELETE Requests79- >>> request.delete(url, params={}, headers={}, auth=None)80+ >>> request.delete(url, params={}, headers={}, cookies=None, auth=None)81 <request object>82 83 84 **Responses:**53 unmodified lines