From ee43ad249798c3d88db809ca68d356a4a06ce5c6 Mon Sep 17 00:00:00 2001 From: robmadole Date: Tue, 1 Mar 2011 09:15:14 -0600 Subject: [PATCH 01/15] Ignore those compiled files --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index e5cfd99e..88e2b4f5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ MANIFEST coverage.xml nosetests.xml -pylint.txt \ No newline at end of file +pylint.txt +*.pyc From e4c690e7bcf90c78bc052b52b284c1fa599b1f8f Mon Sep 17 00:00:00 2001 From: robmadole Date: Tue, 1 Mar 2011 09:21:00 -0600 Subject: [PATCH 02/15] Initial support for cookies --- requests/core.py | 12 +++++++++--- test_requests.py | 10 ++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/requests/core.py b/requests/core.py index b426a4e9..b88d0dcb 100644 --- a/requests/core.py +++ b/requests/core.py @@ -111,6 +111,9 @@ class Request(object): _handlers = [] + if self.cookiejar != None: + _handlers.append(urllib2.HTTPCookieProcessor(self.cookiejar)) + if self.auth: if not isinstance(self.auth.handler, (urllib2.AbstractBasicAuthHandler, urllib2.AbstractDigestAuthHandler)): auth_manager.add_password(self.auth.realm, self.url, self.auth.username, self.auth.password) @@ -119,6 +122,7 @@ class Request(object): _handlers.append(self.auth.handler) + if _handlers: _handlers.extend(get_handlers()) opener = urllib2.build_opener(*_handlers) return opener.open @@ -183,7 +187,10 @@ class Request(object): if not self.sent or anyway: try: opener = self._get_opener() - resp = opener(req) + resp = opener(req) + + if self.cookiejar != None: + self.cookiejar.extract_cookies(resp, req) except urllib2.HTTPError, why: self._build_response(why) self.response.error = why @@ -195,7 +202,6 @@ class Request(object): else: self.response.cached = True - self.sent = self.response.ok return self.sent @@ -406,7 +412,7 @@ def request(method, url, **kwargs): data = kwargs.pop('data', dict()) or kwargs.pop('params', dict()) r = Request(method=method, url=url, data=data, headers=kwargs.pop('headers', {}), - cookiejar=kwargs.pop('cookies', None), files=kwargs.pop('files', None), + cookiejar=kwargs.pop('cookiejar', None), files=kwargs.pop('files', None), auth=kwargs.pop('auth', auth_manager.get_auth(url))) r.send() diff --git a/test_requests.py b/test_requests.py index 22716f13..bcf2b9a3 100644 --- a/test_requests.py +++ b/test_requests.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import unittest +import cookielib import requests @@ -103,6 +104,15 @@ class RequestsTestSuite(unittest.TestCase): self.assertFalse(r.error) r.raise_for_status() + def test_cookie_jar(self): + """ + .. todo:: This really doesn't test to make sure the cookie is working + """ + jar = cookielib.CookieJar() + self.assertFalse(jar) + + requests.get('http://google.com', cookies=jar) + self.assertTrue(jar) From c6acfef3d08c80d36ba33d60c3925265ca53ea35 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 6 Mar 2011 18:48:14 -0500 Subject: [PATCH 03/15] Added Justin Murphy to AUTHORS --- AUTHORS | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 217400d0..0de08422 100644 --- a/AUTHORS +++ b/AUTHORS @@ -13,4 +13,5 @@ Patches and Suggestions - Various Pocoo Members - Chris Adams - Flavio Percoco Premoli -- Dj Gilcrease \ No newline at end of file +- Dj Gilcrease +- Justin Murphy \ No newline at end of file From 9fdee250de7e1aa0c92db5fb435cf1f2f8efe7e7 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 6 Mar 2011 18:48:44 -0500 Subject: [PATCH 04/15] Better POSTER header compatibility (Fixes #13) --- requests/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requests/core.py b/requests/core.py index b426a4e9..22c25d22 100644 --- a/requests/core.py +++ b/requests/core.py @@ -178,7 +178,7 @@ class Request(object): req = _Request(self.url, data=self._enc_data, method=self.method) if self.headers: - req.headers = self.headers + req.headers.update(self.headers) if not self.sent or anyway: try: From e17111a5dd3049a614cbb2ad2db2bf1c4f72edfd Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 6 Mar 2011 18:50:28 -0500 Subject: [PATCH 05/15] testing requests with poster and adding headers --- test_requests.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test_requests.py b/test_requests.py index 22716f13..177ed9f0 100644 --- a/test_requests.py +++ b/test_requests.py @@ -84,6 +84,17 @@ class RequestsTestSuite(unittest.TestCase): post2 = requests.post(bin.url, files={'some': open('test_requests.py')}, data={'some': 'data'}) self.assertEqual(post2.status_code, 201) + + def test_POSTBIN_GET_POST_FILES_WITH_HEADERS(self): + + bin = requests.post('http://www.postbin.org/') + self.assertEqual(bin.status_code, 200) + + post2 = requests.post(bin.url, files={'some': open('test_requests.py')}, + headers={'User-Agent': 'requests-tests'}) + + self.assertEqual(post2.status_code, 201) + def test_nonzero_evaluation(self): r = requests.get('http://google.com/some-404-url') self.assertEqual(bool(r), False) From 1d021c5cc1fe3d8757834d09d265fe76a01fd911 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 6 Mar 2011 19:07:29 -0500 Subject: [PATCH 06/15] Added Rob Madole to AUTHORS --- AUTHORS | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 0de08422..376e0577 100644 --- a/AUTHORS +++ b/AUTHORS @@ -14,4 +14,5 @@ Patches and Suggestions - Chris Adams - Flavio Percoco Premoli - Dj Gilcrease -- Justin Murphy \ No newline at end of file +- Justin Murphy +- Rob Madole \ No newline at end of file From a67cc5c5a98e79545caa89f665bf1528c0c832f4 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 6 Mar 2011 19:19:22 -0500 Subject: [PATCH 07/15] someone stole the cookie jar --- requests/core.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/requests/core.py b/requests/core.py index 80ff5f8a..a685e379 100644 --- a/requests/core.py +++ b/requests/core.py @@ -111,7 +111,7 @@ class Request(object): _handlers = [] - if self.cookiejar != None: + if self.cookiejar: _handlers.append(urllib2.HTTPCookieProcessor(self.cookiejar)) if self.auth: @@ -189,8 +189,9 @@ class Request(object): opener = self._get_opener() resp = opener(req) - if self.cookiejar != None: + if self.cookiejar: self.cookiejar.extract_cookies(resp, req) + except urllib2.HTTPError, why: self._build_response(why) self.response.error = why @@ -412,7 +413,7 @@ def request(method, url, **kwargs): data = kwargs.pop('data', dict()) or kwargs.pop('params', dict()) r = Request(method=method, url=url, data=data, headers=kwargs.pop('headers', {}), - cookiejar=kwargs.pop('cookiejar', None), files=kwargs.pop('files', None), + cookiejar=kwargs.pop('cookies', None), files=kwargs.pop('files', None), auth=kwargs.pop('auth', auth_manager.get_auth(url))) r.send() @@ -429,7 +430,7 @@ def get(url, params={}, headers={}, cookies=None, auth=None): :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - return request('GET', url, params=params, headers=headers, cookiejar=cookies, auth=auth) + return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth) def head(url, params={}, headers={}, cookies=None, auth=None): @@ -442,7 +443,7 @@ def head(url, params={}, headers={}, cookies=None, auth=None): :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - return request('HEAD', url, params=params, headers=headers, cookiejar=cookies, auth=auth) + return request('HEAD', url, params=params, headers=headers, cookies=cookies, auth=auth) def post(url, data={}, headers={}, files=None, cookies=None, auth=None): @@ -456,7 +457,7 @@ def post(url, data={}, headers={}, files=None, cookies=None, auth=None): :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - return request('POST', url, data=data, headers=headers, files=files, cookiejar=cookies, auth=auth) + return request('POST', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth) def put(url, data='', headers={}, files={}, cookies=None, auth=None): @@ -470,7 +471,7 @@ def put(url, data='', headers={}, files={}, cookies=None, auth=None): :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - return request('PUT', url, data=data, headers=headers, files=files, cookiejar=cookies, auth=auth) + return request('PUT', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth) def delete(url, params={}, headers={}, cookies=None, auth=None): @@ -483,7 +484,7 @@ def delete(url, params={}, headers={}, cookies=None, auth=None): :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - return request('DELETE', url, params=params, headers=headers, cookiejar=cookies, auth=auth) + return request('DELETE', url, params=params, headers=headers, cookies=cookies, auth=auth) From 89192c64f04e9fa49adfc6cf35e10bd98bd64a8e Mon Sep 17 00:00:00 2001 From: Aram Dulyan Date: Wed, 9 Mar 2011 22:31:13 +1100 Subject: [PATCH 08/15] An empty cookiejar evaluates to False, so it needs to be compared to None for the cookie functionality to work. --- requests/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/requests/core.py b/requests/core.py index a685e379..252d8b6b 100644 --- a/requests/core.py +++ b/requests/core.py @@ -111,7 +111,7 @@ class Request(object): _handlers = [] - if self.cookiejar: + if self.cookiejar is not None: _handlers.append(urllib2.HTTPCookieProcessor(self.cookiejar)) if self.auth: @@ -189,7 +189,7 @@ class Request(object): opener = self._get_opener() resp = opener(req) - if self.cookiejar: + if self.cookiejar is not None: self.cookiejar.extract_cookies(resp, req) except urllib2.HTTPError, why: From 48ffb627fed0d76806f25fa934a8142be2cf8a87 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Wed, 9 Mar 2011 13:22:47 -0500 Subject: [PATCH 09/15] added Aram Dulyan to authors --- AUTHORS | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 376e0577..96c35301 100644 --- a/AUTHORS +++ b/AUTHORS @@ -15,4 +15,5 @@ Patches and Suggestions - Flavio Percoco Premoli - Dj Gilcrease - Justin Murphy -- Rob Madole \ No newline at end of file +- Rob Madole +- Aram Dulyan \ No newline at end of file From be228043a1eede54c0913166f36ac5b1d99f16be Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Mon, 21 Mar 2011 20:45:37 -0400 Subject: [PATCH 10/15] whitespace fix --- AUTHORS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 96c35301..62a2414d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -16,4 +16,4 @@ Patches and Suggestions - Dj Gilcrease - Justin Murphy - Rob Madole -- Aram Dulyan \ No newline at end of file +- Aram Dulyan From 2401f149750337970300383634f3942e9da3c68f Mon Sep 17 00:00:00 2001 From: robmadole Date: Wed, 30 Mar 2011 00:07:24 -0500 Subject: [PATCH 11/15] Fixing User-agent header problem introduced with Python 2.7.1 --- requests/core.py | 19 ++++++++++++++----- test_requests.py | 14 ++++++++------ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/requests/core.py b/requests/core.py index 252d8b6b..ecb6f23c 100644 --- a/requests/core.py +++ b/requests/core.py @@ -122,13 +122,22 @@ class Request(object): _handlers.append(self.auth.handler) - if _handlers: - _handlers.extend(get_handlers()) - opener = urllib2.build_opener(*_handlers) - return opener.open - else: + if not _handlers: return urllib2.urlopen + _handlers.extend(get_handlers()) + opener = urllib2.build_opener(*_handlers) + + if self.headers: + # Allow default headers in the opener to be overloaded + normal_keys = [k.capitalize() for k in self.headers] + for key, val in opener.addheaders[:]: + if key not in normal_keys: + continue + # Remove it, we have a value to take its place + opener.addheaders.remove((key, val)) + + return opener.open def _build_response(self, resp): """Build internal Response object from given response.""" diff --git a/test_requests.py b/test_requests.py index 97d0e55a..c3759f31 100644 --- a/test_requests.py +++ b/test_requests.py @@ -29,19 +29,24 @@ class RequestsTestSuite(unittest.TestCase): self.assertEqual(r.status_code, 200) def test_HTTP_200_OK_GET_WITH_PARAMS(self): - heads = {'User-agent': 'Mozilla/5.0'} r = requests.get('http://www.google.com/search', params={'q': 'test'}, headers=heads) self.assertEqual(r.status_code, 200) - def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self): - heads = {'User-agent': 'Mozilla/5.0'} r = requests.get('http://google.com/search?test=true', params={'q': 'test'}, headers=heads) self.assertEqual(r.status_code, 200) + + def test_user_agent_transfers(self): + """Issue XX""" + heads = {'User-agent': + 'Mozilla/5.0 (github.com/kennethreitz/requests)'} + + r = requests.get('http://whatsmyua.com', headers=heads); + self.assertTrue(heads['User-agent'] in r.content) def test_HTTP_200_OK_HEAD(self): r = requests.head('http://google.com') @@ -65,7 +70,6 @@ class RequestsTestSuite(unittest.TestCase): requests.auth_manager.empty() def test_POSTBIN_GET_POST_FILES(self): - bin = requests.post('http://www.postbin.org/') print bin.url self.assertEqual(bin.status_code, 200) @@ -77,7 +81,6 @@ class RequestsTestSuite(unittest.TestCase): self.assertEqual(post2.status_code, 201) def test_POSTBIN_GET_POST_FILES_WITH_PARAMS(self): - bin = requests.post('http://www.postbin.org/') self.assertEqual(bin.status_code, 200) @@ -87,7 +90,6 @@ class RequestsTestSuite(unittest.TestCase): def test_POSTBIN_GET_POST_FILES_WITH_HEADERS(self): - bin = requests.post('http://www.postbin.org/') self.assertEqual(bin.status_code, 200) From e350bea167b0bbbd6bc5da6d42036bc560ccde15 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 31 Mar 2011 04:46:19 -0400 Subject: [PATCH 12/15] added Request.read --- requests/core.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requests/core.py b/requests/core.py index ecb6f23c..ca5e5ef6 100644 --- a/requests/core.py +++ b/requests/core.py @@ -160,7 +160,6 @@ class Request(object): else: return url - def send(self, anyway=False): """Sends the request. Returns True of successful, false if not. If there was an HTTPError during transmission, @@ -217,6 +216,8 @@ class Request(object): return self.sent + def read(self): + return self.content class Response(object): """The :class:`Request` object. All :class:`Request` objects contain a From f2b04f94ca689bb4cc1d9f3c4a85476b17dc5c5d Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 31 Mar 2011 04:47:02 -0400 Subject: [PATCH 13/15] Response.read() [file like object] --- requests/core.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/requests/core.py b/requests/core.py index ca5e5ef6..aae09b33 100644 --- a/requests/core.py +++ b/requests/core.py @@ -217,7 +217,7 @@ class Request(object): def read(self): - return self.content + return self.response.read() class Response(object): """The :class:`Request` object. All :class:`Request` objects contain a @@ -249,6 +249,8 @@ class Response(object): if self.error: raise self.error + def read(self): + return self.content class AuthManager(object): From 267a852ba61041f96ac719a4914337f0b49c2f16 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Fri, 1 Apr 2011 14:52:33 -0400 Subject: [PATCH 14/15] Update history --- HISTORY.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/HISTORY.rst b/HISTORY.rst index 49e00f4e..c0b00d69 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,6 +1,14 @@ History ------- +0.3.1 (2011-04-01) +++++++++++++++++++ + +* Cookie Changes +* Response.read() +* Poster fix + + 0.3.0 (2011-02-25) ++++++++++++++++++ From ed8ff63048a5ccaf304e2a59bfed58c9206cfd69 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Fri, 1 Apr 2011 14:53:53 -0400 Subject: [PATCH 15/15] Version bump to 0.3.1 --- requests/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/requests/core.py b/requests/core.py index aae09b33..56c3d061 100644 --- a/requests/core.py +++ b/requests/core.py @@ -24,8 +24,8 @@ from .packages.poster.streaminghttp import register_openers, get_handlers __title__ = 'requests' -__version__ = '0.3.0' -__build__ = 0x000300 +__version__ = '0.3.1' +__build__ = 0x000301 __author__ = 'Kenneth Reitz' __license__ = 'ISC' __copyright__ = 'Copyright 2011 Kenneth Reitz'