urllib3 update
authorKenneth Reitz <me@kennethreitz.com>
Mon, 26 Sep 2011 01:46:07 +0000 (21:46 -0400)
committerKenneth Reitz <me@kennethreitz.com>
Mon, 26 Sep 2011 01:46:07 +0000 (21:46 -0400)
requests/packages/urllib3/connectionpool.py
requests/packages/urllib3/poolmanager.py
requests/packages/urllib3/response.py

index 207702602814b06e17639ea24475812962f95cd7..95e3dd4ad8f7ec5cde56632f44d303c5922caaa2 100644 (file)
@@ -29,6 +29,8 @@ from .exceptions import (
 
 log = logging.getLogger(__name__)
 
+_Default = object()
+
 
 ## Connection objects (extension of httplib)
 
@@ -178,15 +180,19 @@ class HTTPConnectionPool(ConnectionPool):
             log.warning("HttpConnectionPool is full, discarding connection: %s"
                         % self.host)
 
-    def _make_request(self, conn, method, url, **httplib_request_kw):
+    def _make_request(self, conn, method, url, timeout=_Default,
+                      **httplib_request_kw):
         """
         Perform a request on a given httplib connection object taken from our
         pool.
         """
         self.num_requests += 1
 
+        if timeout is _Default:
+            timeout = self.timeout
+
         conn.request(method, url, **httplib_request_kw)
-        conn.sock.settimeout(self.timeout)
+        conn.sock.settimeout(timeout)
         httplib_response = conn.getresponse()
 
         log.debug("\"%s %s %s\" %s %s" %
@@ -202,8 +208,8 @@ class HTTPConnectionPool(ConnectionPool):
                 get_host(url) == (self.scheme, self.host, self.port))
 
     def urlopen(self, method, url, body=None, headers=None, retries=3,
-                redirect=True, assert_same_host=True, pool_timeout=None,
-                release_conn=None, **response_kw):
+                redirect=True, assert_same_host=True, timeout=_Default,
+                pool_timeout=None, release_conn=None, **response_kw):
         """
         Get a connection from the pool and perform an HTTP request.
 
@@ -233,6 +239,9 @@ class HTTPConnectionPool(ConnectionPool):
             consistent else will raise HostChangedError. When False, you can
             use the pool on an HTTP proxy and request foreign hosts.
 
+        timeout
+            If specified, overrides the default timeout for this one request.
+
         pool_timeout
             If set and the pool is set to block=True, then this method will
             block for ``pool_timeout`` seconds and raise EmptyPoolError if no
@@ -273,6 +282,7 @@ class HTTPConnectionPool(ConnectionPool):
         try:
             # Make the request on the httplib connection object
             httplib_response = self._make_request(conn, method, url,
+                                                  timeout=timeout,
                                                   body=body, headers=headers)
             # Import httplib's response into our own wrapper object
             response = HTTPResponse.from_httplib(httplib_response,
index d451f68adb6bf87ea3bf7a914988848f2c52c9dc..1863696700f1f69b14b34bc410df679bbd650ec0 100644 (file)
@@ -34,18 +34,14 @@ class PoolManager(object):
         self.pools = RecentlyUsedContainer(num_pools)
         self.recently_used_pools = []
 
-    def connection_from_url(self, url):
+    def connection_from_host(self, host, port=80, scheme='http'):
         """
-        Similar to connectionpool.connection_from_url but doesn't pass any
-        additional keywords to the ConnectionPool constructor. Additional
-        keywords are taken from the PoolManager constructor.
+        Get a ConnectionPool based on the host, port, and scheme.
         """
-        scheme, host, port = get_host(url)
+        pool_key = (scheme, host, port)
 
         # If the scheme, host, or port doesn't match existing open connections,
         # open a new ConnectionPool.
-        pool_key = (scheme, host, port or port_by_scheme.get(scheme, 80))
-
         pool = self.pools.get(pool_key)
         if pool:
             return pool
@@ -58,7 +54,19 @@ class PoolManager(object):
 
         return pool
 
+    def connection_from_url(self, url):
+        """
+        Similar to connectionpool.connection_from_url but doesn't pass any
+        additional keywords to the ConnectionPool constructor. Additional
+        keywords are taken from the PoolManager constructor.
+        """
+        scheme, host, port = get_host(url)
+
+        port = port or port_by_scheme.get(scheme, 80)
+
+        return  self.connection_from_host(host, port=port, scheme=scheme)
+
     def urlopen(self, method, url, **kw):
-        "Same as HTTP(S)ConnectionPool.urlopen"
+        "Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute."
         conn = self.connection_from_url(url)
         return conn.urlopen(method, url, **kw)
index 14b3068223453373cc058cf3e9bc9bee32dcc510..8c847ce117a7a0e12634846dd7e77bd462fa0f3c 100644 (file)
@@ -83,6 +83,7 @@ class HTTPResponse(object):
             return
 
         self._pool._put_conn(self._connection)
+        self._connection = None
 
     @property
     def data(self):