From: moliware Date: Sun, 19 Jun 2011 20:36:50 +0000 (+0200) Subject: Support for proxies X-Git-Tag: v0.6.0~142 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3e7c682e66fb4b276c8343bb718d8b13037cea15;p=services%2Fpython-requests.git Support for proxies --- diff --git a/AUTHORS b/AUTHORS index 1576f9f..f4787ec 100644 --- a/AUTHORS +++ b/AUTHORS @@ -24,3 +24,4 @@ Patches and Suggestions - Zbigniew Siciarz - Daniele Tricoli 'Eriol' - Richard Boulton +- Miguel Olivares diff --git a/requests/api.py b/requests/api.py index cf2e575..f3445e2 100644 --- a/requests/api.py +++ b/requests/api.py @@ -19,7 +19,7 @@ from .models import Request, Response, AuthManager, AuthObject, auth_manager __all__ = ('request', 'get', 'head', 'post', 'put', 'delete') def request(method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, - timeout=None, allow_redirects=False): + timeout=None, allow_redirects=False, proxies=None): """Constructs and sends a :class:`Request `. Returns :class:`Response ` object. :param method: method for the new :class:`Request` object. @@ -32,6 +32,7 @@ def request(method, url, params=None, data=None, headers=None, cookies=None, fil :param auth: (optional) AuthObject to enable Basic HTTP Auth. :param timeout: (optional) Float describing the timeout of the request. :param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed. + :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. """ r = Request( @@ -44,14 +45,15 @@ def request(method, url, params=None, data=None, headers=None, cookies=None, fil files = files, auth = auth or auth_manager.get_auth(url), timeout = timeout or config.settings.timeout, - allow_redirects = allow_redirects + allow_redirects = allow_redirects, + proxies = proxies ) r.send() return r.response -def get(url, params=None, headers=None, cookies=None, auth=None, timeout=None): +def get(url, params=None, headers=None, cookies=None, auth=None, timeout=None, proxies=None): """Sends a GET request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. @@ -60,12 +62,14 @@ def get(url, params=None, headers=None, cookies=None, auth=None, timeout=None): :param cookies: (optional) CookieJar object to send with the :class:`Request`. :param auth: (optional) AuthObject to enable Basic HTTP Auth. :param timeout: (optional) Float describing the timeout of the request. + :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. """ - return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth, timeout=timeout) + return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth, timeout=timeout, + proxies=proxies) -def head(url, params=None, headers=None, cookies=None, auth=None, timeout=None): +def head(url, params=None, headers=None, cookies=None, auth=None, timeout=None, proxies=None): """Sends a HEAD request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. @@ -74,13 +78,15 @@ def head(url, params=None, headers=None, cookies=None, auth=None, timeout=None): :param cookies: (optional) CookieJar object to send with the :class:`Request`. :param auth: (optional) AuthObject to enable Basic HTTP Auth. :param timeout: (optional) Float describing the timeout of the request. + :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. """ - return request('HEAD', url, params=params, headers=headers, cookies=cookies, auth=auth, timeout=timeout) + return request('HEAD', url, params=params, headers=headers, cookies=cookies, auth=auth, timeout=timeout, + proxies=proxies) def post(url, data='', headers=None, files=None, cookies=None, auth=None, - timeout=None, allow_redirects=False, params=None): + timeout=None, allow_redirects=False, params=None, proxies=None): """Sends a POST request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. @@ -92,15 +98,16 @@ def post(url, data='', headers=None, files=None, cookies=None, auth=None, :param timeout: (optional) Float describing the timeout of the request. :param allow_redirects: (optional) Boolean. Set to True if redirect following is allowed. :param params: (optional) Dictionary of parameters, or bytes, to be sent in the query string for the :class:`Request`. + :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. """ return request('POST', url, params=params, data=data, headers=headers, files=files, cookies=cookies, auth=auth, timeout=timeout, - allow_redirects=allow_redirects) + allow_redirects=allow_redirects, proxies=proxies) def put(url, data='', headers=None, files=None, cookies=None, auth=None, - timeout=None, allow_redirects=False, params=None): + timeout=None, allow_redirects=False, params=None, proxies=None): """Sends a PUT request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. @@ -112,14 +119,16 @@ def put(url, data='', headers=None, files=None, cookies=None, auth=None, :param timeout: (optional) Float describing the timeout of the request. :param allow_redirects: (optional) Boolean. Set to True if redirect following is allowed. :param params: (optional) Dictionary of parameters, or bytes, to be sent in the query string for the :class:`Request`. + :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. """ return request('PUT', url, params=params, data=data, headers=headers, files=files, cookies=cookies, auth=auth, timeout=timeout, - allow_redirects=allow_redirects) + allow_redirects=allow_redirects, proxies=proxies) -def delete(url, params=None, headers=None, cookies=None, auth=None, timeout=None, allow_redirects=False): +def delete(url, params=None, headers=None, cookies=None, auth=None, timeout=None, allow_redirects=False, + proxies=None): """Sends a DELETE request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. @@ -129,7 +138,8 @@ def delete(url, params=None, headers=None, cookies=None, auth=None, timeout=None :param auth: (optional) AuthObject to enable Basic HTTP Auth. :param timeout: (optional) Float describing the timeout of the request. :param allow_redirects: (optional) Boolean. Set to True if redirect following is allowed. + :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. """ return request('DELETE', url, params=params, headers=headers, cookies=cookies, auth=auth, - timeout=timeout, allow_redirects=allow_redirects) + timeout=timeout, allow_redirects=allow_redirects, proxies=proxies) diff --git a/requests/models.py b/requests/models.py index 6f19718..e34ef3c 100644 --- a/requests/models.py +++ b/requests/models.py @@ -32,7 +32,8 @@ class Request(object): def __init__(self, url=None, headers=dict(), files=None, method=None, data=dict(), params=dict(), auth=None, cookiejar=None, - timeout=None, redirect=False, allow_redirects=False): + timeout=None, redirect=False, allow_redirects=False, + proxies=None): socket.setdefaulttimeout(timeout) @@ -55,6 +56,8 @@ class Request(object): self.redirect = redirect #: Set to True if full redirects are allowed (e.g. re-POST-ing of data at new ``Location``) self.allow_redirects = allow_redirects + # Dictionary mapping protocol to the URL of the proxy (e.g. {'http': 'foo.bar:3128'}) + self.proxies = proxies self.data, self._enc_data = self._encode_params(data) self.params, self._enc_params = self._encode_params(params) @@ -110,6 +113,8 @@ class Request(object): _handlers.append(self.auth.handler) + if self.proxies: + _handlers.append(urllib2.ProxyHandler(self.proxies)) _handlers.append(HTTPRedirectHandler)