Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for proxies #66

Merged
merged 1 commit into from
Jun 21, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ Patches and Suggestions
- Zbigniew Siciarz
- Daniele Tricoli 'Eriol'
- Richard Boulton
- Miguel Olivares <miguel@moliware.com>
34 changes: 22 additions & 12 deletions requests/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
__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 <models.Request>`. Returns :class:`Response <models.Response>` object.

:param method: method for the new :class:`Request` object.
Expand All @@ -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(
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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)
7 changes: 6 additions & 1 deletion requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -55,6 +56,8 @@ def __init__(self, url=None, headers=dict(), files=None, method=None,
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)
Expand Down Expand Up @@ -110,6 +113,8 @@ def _get_opener(self):

_handlers.append(self.auth.handler)

if self.proxies:
_handlers.append(urllib2.ProxyHandler(self.proxies))

_handlers.append(HTTPRedirectHandler)

Expand Down