mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
Support for proxies
This commit is contained in:
@@ -24,3 +24,4 @@ Patches and Suggestions
|
||||
- Zbigniew Siciarz
|
||||
- Daniele Tricoli 'Eriol'
|
||||
- Richard Boulton
|
||||
- Miguel Olivares <miguel@moliware.com>
|
||||
|
||||
+22
-12
@@ -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 <models.Request>`. Returns :class:`Response <models.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)
|
||||
|
||||
+6
-1
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user