From: Kenneth Reitz Date: Sat, 15 Dec 2012 04:51:53 +0000 (-0500) Subject: logging handlers X-Git-Tag: v1.0.0~76 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4a9f3572a38796d71c5b40f1c506da03161b1c70;p=services%2Fpython-requests.git logging handlers Closes #971 --- diff --git a/requests/__init__.py b/requests/__init__.py index 8e5b6b0..af2e5b1 100644 --- a/requests/__init__.py +++ b/requests/__init__.py @@ -58,3 +58,14 @@ from .exceptions import ( RequestException, Timeout, URLRequired, TooManyRedirects, HTTPError, ConnectionError ) + +# Set default logging handler to avoid "No handler found" warnings. +import logging +try: # Python 2.7+ + from logging import NullHandler +except ImportError: + class NullHandler(logging.Handler): + def emit(self, record): + pass + +logging.getLogger(__name__).addHandler(NullHandler()) diff --git a/requests/defaults.py b/requests/defaults.py index d8bcb29..0d4e8e7 100644 --- a/requests/defaults.py +++ b/requests/defaults.py @@ -38,5 +38,4 @@ defaults['max_retries'] = 0 defaults['keep_alive'] = True defaults['encode_uri'] = True defaults['trust_env'] = True -defaults['store_cookies'] = True defaults['support_http0.9'] = True diff --git a/requests/models.py b/requests/models.py index d7a910e..4527f6c 100644 --- a/requests/models.py +++ b/requests/models.py @@ -10,6 +10,8 @@ This module contains the primary objects that power Requests. import os import socket import collections +import logging + from datetime import datetime from io import BytesIO @@ -41,6 +43,7 @@ from .compat import ( REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved) CONTENT_CHUNK_SIZE = 10 * 1024 +log = logging.getLogger(__name__) class Request(object): """The :class:`Request ` object. It carries out all functionality @@ -503,10 +506,7 @@ class Request(object): self.__dict__.update(r.__dict__) # Logging - if self.config.get('verbose'): - self.config.get('verbose').write('%s %s %s\n' % ( - datetime.now().isoformat(), self.method, url - )) + log.info('Sending %s: %s' % (self, url)) # Use .netrc auth if none was provided. if not self.auth and self.config.get('trust_env'): diff --git a/tests/test_cookies.py b/tests/test_cookies.py index e1c4203..90141a9 100755 --- a/tests/test_cookies.py +++ b/tests/test_cookies.py @@ -104,28 +104,6 @@ class CookieTests(TestBaseMixin, unittest.TestCase): insecure_cookies_sent = json.loads(insecure_resp.text)['cookies'] self.assertEqual(insecure_cookies_sent, {}) - def test_disabled_cookie_persistence(self): - """Test that cookies are not persisted when configured accordingly.""" - - config = {'store_cookies': False} - - # Check the case when no cookie is passed as part of the request and the one in response is ignored - cookies = requests.get(httpbin('cookies', 'set', 'key', 'value'), config=config).cookies - self.assertTrue(cookies.get("key") is None) - - # Test that the cookies passed while making the request still gets used and is available in response object. - # only the ones received from server is not saved - cookies_2 = requests.get(httpbin('cookies', 'set', 'key', 'value'), config=config,\ - cookies={"key_2": "value_2"}).cookies - self.assertEqual(len(cookies_2), 1) - self.assertEqual(cookies_2.get("key_2"), "value_2") - - # Use the session and make sure that the received cookie is not used in subsequent calls - s = requests.session() - s.get(httpbin('cookies', 'set', 'key', 'value'), config=config) - r = s.get(httpbin('cookies')) - self.assertEqual(json.loads(r.text)['cookies'], {}) - def test_jar_utility_functions(self): """Test utility functions such as list_domains, list_paths, multiple_domains.""" r = requests.get("http://github.com")