Skip to content
Myr

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

3 files+69 added−22 removed

modified:requests/core.py+53 added-15 removed
11 unmodified lines
1213import urllib14import urllib2151617try:18 import eventlet19 eventlet.monkey_patch()20except ImportError:21 pass2223if not 'eventlet' in locals():24 try:25 from gevent import monkey26 monkey.patch_all()27 except ImportError:28 pass293031from .packages.poster.encode import multipart_encode32from .packages.poster.streaminghttp import register_openers3334
38 unmodified lines
73 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 lines
92 object.__setattr__(self, name, value)93 94 95 def _checks(self):79 """Deterministic checks for consistiency."""96 """Deterministic checks for consistency."""9798 if not self.url:99 raise URLRequired100101 102 def _get_opener(self):103 """Creates appropriate opener object for urllib2."""87 88 if self.auth:10490 # create a password manager91 authr = urllib2.HTTPPasswordMgrWithDefaultRealm()105 _handlers = []10693 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:10897 # use the opener to fetch a URL109 if self.auth:110111 authr = urllib2.HTTPPasswordMgrWithDefaultRealm()112113 authr.add_password(None, self.url, self.auth.username, self.auth.password)114 auth_handler = urllib2.HTTPBasicAuthHandler(authr)115116 _handlers.append(auth_handler)117118 if self.cookiejar:119120 cookie_handler = urllib2.HTTPCookieProcessor(cookiejar)121 _handlers.append(cookie_handler)122123 opener = urllib2.build_opener(*_handlers)124 return opener.open125126 else:127 return urllib2.urlopen128129
4 unmodified lines
134 self.response.headers = resp.info().dict135 self.response.content = resp.read()136 self.response.url = resp.url137138 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 lines
282 self.password = password283284285258def get(url, params={}, headers={}, auth=None):286def get(url, params={}, headers={}, cookies=None, auth=None):287 """Sends a GET request. Returns :class:`Response` object.288289 :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 line
298 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.response308309280def head(url, params={}, headers={}, auth=None):310def head(url, params={}, headers={}, cookies=None, auth=None):311 """Sends a HEAD request. Returns :class:`Response` object.312313 :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 lines
323 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.response333334303def post(url, data={}, headers={}, files=None, auth=None):335def post(url, data={}, headers={}, files=None, cookies=None, auth=None):336 """Sends a POST request. Returns :class:`Response` object.337338 :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 lines
352 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 330def put(url, data='', headers={}, files={}, auth=None):364def put(url, data='', headers={}, files={}, cookies=None, auth=None):365 """Sends a PUT request. Returns :class:`Response` object.366367 :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 lines
378 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.response388389 354def delete(url, params={}, headers={}, auth=None):390def delete(url, params={}, headers={}, cookies=None, auth=None):391 """Sends a DELETE request. Returns :class:`Response` object.392393 :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 line
402 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 lines
modified:HISTORY.rst+8 added
1History2-------340.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)91011120.2.1 (2011-02-14)13++++++++++++++++++1415* Added file attribute to POST and PUT requests for multipart-encode file uploads.
15 unmodified lines
modified:README.rst+8 added-7 removed
54 unmodified lines
55 56**Requests:**5758All request functions return a Response object (see below).5960If a {filename: fileobject} dictionary is passed in (files=...), a multipart_encode upload will be performed.61If 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 8384**Responses:**
53 unmodified lines