Compare commits

...

42 Commits

Author SHA1 Message Date
Kenneth Reitz
10eed6344c v0.8.4 2011-12-11 11:39:57 -05:00
Kenneth Reitz
06eee390dd updated notice 2011-12-11 11:39:11 -05:00
Kenneth Reitz
fad9abfd77 Merge remote-tracking branch 'origin/develop' into develop 2011-12-11 11:26:16 -05:00
Kenneth Reitz
a5f79712bb Fix for #303 2011-12-11 11:26:13 -05:00
Kenneth Reitz
c71ac7917a Test for #303 2011-12-11 11:22:33 -05:00
Kenneth Reitz
4e59f9ec93 Merge pull request #305 from oasisbob/develop
Fix spelling errors, typos.
2011-12-11 08:04:26 -08:00
Nick Hatch
a6412b02ba Fix spelling errors, typos. 2011-12-11 01:58:55 -06:00
Kenneth Reitz
961f78ab5f Merge remote-tracking branch 'origin/develop' into develop 2011-12-09 13:23:26 -08:00
Kenneth Reitz
463ca76cc4 colors! 2011-12-09 13:23:16 -08:00
Kenneth Reitz
f58ab8bc03 Update AUTHORS.rst 2011-12-09 08:43:02 -08:00
Kenneth Reitz
3a06c35da3 Merge pull request #301 from cwvh/patch-1
Fixed typo in documentation.
2011-12-09 08:42:18 -08:00
Chris Van Horne
37070ac30e Fixed spelling error in documentation. 2011-12-09 00:48:22 -07:00
Kenneth Reitz
e56d05d230 Update AUTHORS.rst 2011-12-06 18:41:20 -08:00
Kenneth Reitz
f1fa8f0064 Merge pull request #300 from edsu/develop
disable_redirects should be allow_redirects in docs
2011-12-06 18:38:31 -08:00
Ed Summers
56d6d703f9 disable_redirects parameter does not seem to exist anymore, updated docs to point folks at allow_redirects 2011-12-06 21:36:32 -05:00
Kenneth Reitz
7fbbdeb324 Merge remote-tracking branch 'origin/develop' into develop 2011-11-30 12:45:17 -05:00
Kenneth Reitz
f7e4832472 sp 2011-11-30 12:45:13 -05:00
Kenneth Reitz
4ed16d99c2 Merge pull request #294 from failberg/develop
Documentation fix: clarification of keep-alive behavior.
2011-11-29 00:06:54 -08:00
Bryan Berg
02f422d132 Clarification of keep-alive behavior.
The existing behavior was a little opaque for me--I was making a PUT request which returned a HTTP 204 with a 0-byte body. I was never reading the body and was wondering why my connections were not being reused.
2011-11-28 22:55:02 -08:00
Kenneth Reitz
a76f4925b5 didn't mean to save that 2011-11-27 15:07:05 -05:00
Kenneth Reitz
c2e942f230 no .idea ignore 2011-11-27 11:31:48 -05:00
Kenneth Reitz
c7b99f7819 Set Response.sent to True
Fixes #291
2011-11-27 11:29:52 -05:00
Kenneth Reitz
6bb4c4ebe6 Added test for Request.sent.
#291
2011-11-27 11:24:59 -05:00
Kenneth Reitz
ee36baf4f3 test tuple'd auth 2011-11-27 10:41:06 -05:00
Kenneth Reitz
e85b857a35 no link to self 2011-11-27 10:37:14 -05:00
Kenneth Reitz
e7ed1224d2 flattr 2011-11-27 10:32:36 -05:00
Kenneth Reitz
9f29a31f66 v0.8.3 2011-11-27 10:08:19 -05:00
Kenneth Reitz
356bf42373 v0.8.3 2011-11-27 10:06:39 -05:00
Kenneth Reitz
ba00c4411c add github buttons to sidebars 2011-11-27 10:00:47 -05:00
Kenneth Reitz
c74b66a53e v0.8.3 2011-11-27 10:00:40 -05:00
Kenneth Reitz
a7a619ba5c Merge branch 'auth_callables' of https://github.com/idangazit/requests into develop
Conflicts:
	requests/models.py
2011-11-26 10:14:14 -05:00
Kenneth Reitz
65c06579b4 Update AUTHORS.rst 2011-11-26 10:10:57 -05:00
Kenneth Reitz
a5ff4be7b0 Merge pull request #283 from rgieseke/develop
Display full URL when logging.
2011-11-26 07:10:08 -08:00
Kenneth Reitz
52ac24120c Merge pull request #287 from sharat87/patch-2
Allow generators or any iterators for async.map
2011-11-26 07:09:37 -08:00
Kenneth Reitz
a77b91671c Merge pull request #288 from sharat87/session-arg
Add use_session argument to request method.
2011-11-25 20:04:24 -08:00
Shrikant Sharat
a350ceffb9 Rename use_session to session. 2011-11-25 11:41:40 +05:30
Shrikant Sharat
5c72601599 Add use_session argument to request method.
This allows usage of an existing session to make a request with the
request and the get, post etc. function.
2011-11-25 08:32:07 +05:30
Shrikant Sharat Kandula
99b6e35940 Allow generators or any iterators for async.map 2011-11-25 07:45:12 +05:30
Robert Gieseke
8ae4b440e3 Display full URL when logging. 2011-11-20 10:14:51 +01:00
Idan Gazit
1933d3c707 Added support for HTTP Basic Auth credentials in 2-tuple 2011-11-19 23:59:27 +02:00
Kenneth Reitz
9d8d1d14f1 package 2011-11-19 16:28:22 -05:00
Idan Gazit
7fdb09b766 Converted auth to use callable objects instead of tuples.
My attempt to address #275 on kennethreitz/requests.
2011-11-17 13:44:13 +02:00
25 changed files with 244 additions and 132 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,3 @@
.idea
MANIFEST
coverage.xml
nosetests.xml

View File

@@ -60,4 +60,8 @@ Patches and Suggestions
- Juergen Brendel
- Juan Riaza
- Ryan Kelly
- Rolando Espinoza La fuente
- Rolando Espinoza La fuente
- Robert Gieseke
- Idan Gazit
- Ed Summers
- Chris Van Horne

View File

@@ -1,6 +1,19 @@
History
-------
0.8.4 (2011-12-11)
++++++++++++++++++
* Prefetch bugfix.
* Added license to installed version.
0.8.3 (2011-11-27)
++++++++++++++++++
* Converted auth system to use simpler callable objects.
* New session parameter to API methods.
* Display full URL while logging.
0.8.2 (2011-11-19)
++++++++++++++++++

View File

@@ -1 +1 @@
include README.rst LICENSE HISTORY.rst test_requests.py
include README.rst LICENSE NOTICE HISTORY.rst test_requests.py

View File

@@ -2,7 +2,7 @@ init:
pip install -r reqs.txt
test:
nosetests test_requests.py
nosetests --with-color test_requests.py
ci: init
nosetests test_requests.py --with-xunit --xunit-file=junit-report.xml

26
NOTICE
View File

@@ -1,12 +1,26 @@
Request includes some vendorized python libraries to ease installation.
Poster License
==============
Urllib3 License
===============
Copyright (c) 2010 Chris AtLee
This is the MIT license: http://www.opensource.org/licenses/mit-license.php
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Copyright 2008-2011 Andrey Petrov and contributors (see CONTRIBUTORS.txt),
Modifications copyright 2022 Kenneth Reitz.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -4,12 +4,28 @@
</a>
</p>
<p>
<iframe src="http://markdotto.github.com/github-buttons/github-btn.html?user=kennethreitz&repo=requests&type=watch&count=true&size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="200px" height="35px"></iframe>
</p>
<p>
Requests is an elegant and simple HTTP library for Python, built for
human beings. You are currently looking at the documentation of the
development release.
</p>
<h3>Support Requests</h3>
<p>
If you love Requests, consider making a small donation <a href="https://flattr.com/thing/442264/Requests">on Flattr</a>:
</p>
<p>
<a class="FlattrButton" style="display:none;" rev="flattr;button:compact;" href="http://docs.python-requests.org/"></a>
<noscript><a href="http://flattr.com/thing/442264/Requests" target="_blank">
<img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" /></a></noscript>
</p>
<h3>Feedback</h3>
<p>
Feedback is greatly appreciated. If you have any questions, comments,
@@ -20,8 +36,8 @@
<h3>Useful Links</h3>
<ul>
<li><a href="http://python-requests.org/">The Requests Website</a></li>
<li><a href="http://pypi.python.org/pypi/requests">Requests @ PyPI</a></li>
<li><a href="http://github.com/kennethreitz/requests">Requests @ GitHub</a></li>
<li><a href="https://flattr.com/thing/442264/Requests">Requests @ Flattr</a></li>
<li><a href="http://github.com/kennethreitz/requests/issues">Issue Tracker</a></li>
</ul>

View File

@@ -3,9 +3,23 @@
<img class="logo" src="{{ pathto('_static/requests-sidebar.png', 1) }}" alt="Logo"/>
</a>
</p>
<p>
<iframe src="http://markdotto.github.com/github-buttons/github-btn.html?user=kennethreitz&repo=requests&type=watch&count=true&size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="200px" height="35px"></iframe>
</p>
<p>
Requests is an elegant and simple HTTP library for Python, built for
human beings. You are currently looking at the documentation of the
development release.
</p>
<h3>Support Requests</h3>
<p>
If you love Requests, consider making a small donation <a href="https://flattr.com/thing/442264/Requests">on Flattr</a>:
</p>
<p>
<a class="FlattrButton" style="display:none;" rev="flattr;button:compact;" href="http://docs.python-requests.org/"></a>
<noscript><a href="http://flattr.com/thing/442264/Requests" target="_blank">
<img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" /></a></noscript>
</p>

View File

@@ -2,7 +2,7 @@ krTheme Sphinx Style
====================
This repository contains sphinx styles Kenneth Reitz uses in most of
his projects. It is a drivative of Mitsuhiko's themes for Flask and Flask related
his projects. It is a derivative of Mitsuhiko's themes for Flask and Flask related
projects. To use this style in your Sphinx documentation, follow
this guide:

View File

@@ -14,6 +14,17 @@
<a href="https://github.com/kennethreitz/requests" class="github">
<img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" class="github"/>
</a>
<script type="text/javascript">
/* <![CDATA[ */
(function() {
var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://api.flattr.com/js/0.6/load.js?mode=auto';
t.parentNode.insertBefore(s, t);
})();
/* ]]> */
</script>
<script type="text/javascript">
var _gaq = _gaq || [];

View File

@@ -38,7 +38,7 @@ Testimonials
`Twitter, Inc <http://twitter.com>`_,
`Readability <http://readability.com>`_, and
Federal US Institutions
use Requests internally. It has been installed over 45,000 times from PyPi.
use Requests internally. It has been installed over 45,000 times from PyPI.
**Armin Ronacher**
Requests is the perfect example how beautiful an API can be with the

View File

@@ -10,7 +10,7 @@ Session Objects
---------------
The Session object allows you to persist certain parameters across
requests. It also perstists cookies across all requests made from the
requests. It also persists cookies across all requests made from the
Session instance.
A session object has all the methods of the main Requests API.
@@ -49,7 +49,7 @@ All values that are contained within a session are directly available to you. Se
Body Content Workflow
----------------------
By default, When you make a request, the body of the response isn't downloaded immediately. The response headers are downloaded when you make a request, but the content isn't downloaded until you access the :class:`Response.content` attribute.
By default, when you make a request, the body of the response isn't downloaded immediately. The response headers are downloaded when you make a request, but the content isn't downloaded until you access the :class:`Response.content` attribute.
Let's walk through it::
@@ -80,6 +80,8 @@ Keep-Alive
Excellent news — thanks to urllib3, keep-alive is 100% automatic within a session! Any requests that you make within a session will automatically reuse the appropriate connection!
Note that connections are only released back to the pool for reuse once all body data has been read; be sure to either set ``prefetch`` to ``True`` or read the ``content`` property of the ``Response`` object.
If you'd like to disable keep-alive, you can simply set the ``keep_alive`` configuration to ``False``::
s = requests.session()
@@ -89,8 +91,8 @@ If you'd like to disable keep-alive, you can simply set the ``keep_alive`` confi
Asynchronous Requests
----------------------
Requests has first-class support for concurrent requests, powered
by gevent. This allows you to send a bunch of HTTP requests at the same
Requests has first-class support for concurrent requests, powered by gevent.
This allows you to send a bunch of HTTP requests at the same time.
First, let's import the async module. Heads up — if you don't have
`gevent <http://pypi.python.org/pypi/gevent>`_ this will fail::
@@ -122,7 +124,7 @@ will also guarantee execution of the ``response`` hook, described below. ::
.. admonition:: Throttling
The ``map`` function also takes a ``size`` parameter, that specifies the nubmer of connections to make at a time::
The ``map`` function also takes a ``size`` parameter, that specifies the number of connections to make at a time::
async.map(rs, size=5)
@@ -211,32 +213,33 @@ Custom Authentication
Requests allows you to use specify your own authentication mechanism.
When you pass our authentication tuple to a request method, the first
string is the type of authentication. 'basic' is inferred if none is
provided.
Any callable which is passed as the ``auth`` argument to a request method will
have the opportunity to modify the request before it is dispatched.
You can pass in a callable object instead of a string for the first item
in the tuple, and it will be used in place of the built in authentication
callbacks.
Authentication implementations are subclasses of ``requests.auth.AuthBase``,
and are easy to define. Requests provides two common authentication scheme
implementations in ``requests.auth``: ``HTTPBasicAuth`` and ``HTTPDigestAuth``.
Let's pretend that we have a web service that will only respond if the
``X-Pizza`` header is set to a password value. Unlikely, but just go with it.
We simply need to define a callback function that will be used to update the
Request object, right before it is dispatched.
::
def pizza_auth(r, username):
"""Attaches HTTP Pizza Authentication to the given Request object.
"""
r.headers['X-Pizza'] = username
from requests.auth import AuthBase
class PizzaAuth(AuthBase):
"""Attaches HTTP Pizza Authentication to the given Request object."""
def __init__(self, username):
# setup any auth-related data here
self.username = username
return r
def __call__(self, r):
# modify and return the request
r.headers['X-Pizza'] = self.username
return r
Then, we can make a request using our Pizza Auth::
>>> requests.get('http://pizzabin.org/admin', auth=(pizza_auth, 'kenneth'))
>>> requests.get('http://pizzabin.org/admin', auth=PizzaAuth('kenneth'))
<Response [200]>

View File

@@ -265,29 +265,42 @@ authentication, but the most common is HTTP Basic Auth.
Making requests with Basic Auth is extremely simple::
>>> from requests.auth import HTTPBasicAuth
>>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
<Response [200]>
Due to the prevalence of HTTP Basic Auth, requests provides a shorthand for
this authentication method::
>>> requests.get('https://api.github.com/user', auth=('user', 'pass'))
<Response [200]>
Providing the credentials as a tuple in this fashion is functionally equivalent
to the ``HTTPBasicAuth`` example above.
Digest Authentication
---------------------
Another popular form of web service protection is Digest Authentication::
>>> from requests.auth import HTTPDigestAuth
>>> url = 'http://httpbin.org/digest-auth/auth/user/pass'
>>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))
<Response [200]>
OAuth Authentication
--------------------
Miguel Araujo's `requests-oauth <http://pypi.python.org/pypi/requests-oauth>`_ project provides a simple interface for
establishing OAuth connections. Documentation and examples can be found on the requests-oauth `git repository <https://github.com/maraujop/requests-oauth>`_.
Digest Authentication
---------------------
Another popular form of web service protection is Digest Authentication::
>>> url = 'http://httpbin.org/digest-auth/auth/user/pass'
>>> requests.get(url, auth=('digest', 'user', 'pass'))
<Response [200]>
Redirection and History
-----------------------
Requests will automatically perform location redirection while using impodotent methods.
Requests will automatically perform location redirection while using idempotent methods.
GitHub redirects all HTTP requests to HTTPS. Let's see what happens::
@@ -303,9 +316,9 @@ The :class:`Response.history` list contains a list of the
:class:`Request` objects that were created in order to complete the request.
If you're using GET, HEAD, or OPTIONS, you can disable redirection
handling with the ``disable_redirects`` parameter::
handling with the ``allow_redirects`` parameter::
>>> r = requests.get('http://github.com')
>>> r = requests.get('http://github.com', allow_redirects=False)
>>> r.status_code
301
>>> r.history
@@ -332,7 +345,7 @@ You can tell requests to stop waiting for a response after a given number of sec
.. admonition:: Note
``timeout`` only effects the connection process itself, not the downloading of the respone body.
``timeout`` only effects the connection process itself, not the downloading of the response body.
Errors and Exceptions

View File

@@ -4,4 +4,4 @@ gunicorn
nose
pyflakes
omnijson
rudolf
rudolf2

View File

@@ -15,13 +15,14 @@ requests
"""
__title__ = 'requests'
__version__ = '0.8.2'
__build__ = 0x000802
__version__ = '0.8.4'
__build__ = 0x000804
__author__ = 'Kenneth Reitz'
__license__ = 'ISC'
__copyright__ = 'Copyright 2011 Kenneth Reitz'
from . import utils
from .models import Request, Response
from .api import request, get, head, post, patch, put, delete, options

View File

@@ -11,7 +11,7 @@ This module implements the Requests API.
"""
from .sessions import session
from . import sessions
def request(method, url,
@@ -27,6 +27,7 @@ def request(method, url,
hooks=None,
return_response=True,
prefetch=False,
session=None,
config=None):
"""Constructs and sends a :class:`Request <Request>`.
Returns :class:`Response <Response>` object.
@@ -38,15 +39,16 @@ def request(method, url,
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
:param files: (optional) Dictionary of 'name': file-like-objects (or {'name': ('filename', fileobj)}) for multipart encoding upload.
:param auth: (optional) Auth typle to enable Basic/Digest/Custom HTTP Auth.
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom 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.
:param return_response: (optional) If False, an un-sent Request object will returned.
:param session: (optional) A :class:`Session` object to be used for the request.
:param config: (optional) A configuration dictionary.
"""
s = session()
s = session or sessions.session()
return s.request(
method=method,
url=url,

View File

@@ -71,6 +71,8 @@ def map(requests, prefetch=True, size=None):
:param size: Specifies the number of requests to make at a time. If None, no throttling occurs.
"""
requests = list(requests)
if size:
pool = Pool(size)
pool.map(send, requests)

View File

@@ -16,26 +16,32 @@ from urlparse import urlparse
from .utils import randombytes, parse_dict_header
def http_basic(r, username, password):
"""Attaches HTTP Basic Authentication to the given Request object.
Arguments should be considered non-positional.
class AuthBase(object):
"""Base class that all auth implementations derive from"""
"""
username = str(username)
password = str(password)
auth_s = b64encode('%s:%s' % (username, password))
r.headers['Authorization'] = ('Basic %s' % auth_s)
return r
def __call__(self, r):
raise NotImplementedError('Auth hooks must be callable.')
def http_digest(r, username, password):
"""Attaches HTTP Digest Authentication to the given Request object.
Arguments should be considered non-positional.
"""
class HTTPBasicAuth(AuthBase):
"""Attaches HTTP Basic Authentication to the given Request object."""
def __init__(self, username, password):
self.username = str(username)
self.password = str(password)
def handle_401(r):
def __call__(self, r):
auth_s = b64encode('%s:%s' % (self.username, self.password))
r.headers['Authorization'] = ('Basic %s' % auth_s)
return r
class HTTPDigestAuth(AuthBase):
"""Attaches HTTP Digest Authentication to the given Request object."""
def __init__(self, username, password):
self.username = username
self.password = password
def handle_401(self, r):
"""Takes the given response and tries digest-auth, if needed."""
s_auth = r.headers.get('www-authenticate', '')
@@ -70,7 +76,7 @@ def http_digest(r, username, password):
p_parsed = urlparse(r.request.url)
path = p_parsed.path + p_parsed.query
A1 = "%s:%s:%s" % (username, realm, password)
A1 = "%s:%s:%s" % (self.username, realm, self.password)
A2 = "%s:%s" % (r.request.method, path)
if qop == 'auth':
@@ -95,7 +101,7 @@ def http_digest(r, username, password):
# XXX should the partial digests be encoded too?
base = 'username="%s", realm="%s", nonce="%s", uri="%s", ' \
'response="%s"' % (username, realm, nonce, path, respdig)
'response="%s"' % (self.username, realm, nonce, path, respdig)
if opaque:
base += ', opaque="%s"' % opaque
if entdig:
@@ -104,7 +110,6 @@ def http_digest(r, username, password):
if qop:
base += ', qop=auth, nc=%s, cnonce="%s"' % (ncvalue, cnonce)
r.request.headers['Authorization'] = 'Digest %s' % (base)
r.request.send(anyway=True)
_r = r.request.response
@@ -114,33 +119,6 @@ def http_digest(r, username, password):
return r
r.hooks['response'] = handle_401
return r
def dispatch(t):
"""Given an auth tuple, return an expanded version."""
if not t:
return t
else:
t = list(t)
# Make sure they're passing in something.
assert len(t) >= 2
# If only two items are passed in, assume HTTPBasic.
if (len(t) == 2):
t.insert(0, 'basic')
# Allow built-in string referenced auths.
if isinstance(t[0], basestring):
if t[0] in ('basic', 'forced_basic'):
t[0] = http_basic
elif t[0] in ('digest',):
t[0] = http_digest
# Return a custom callable.
return (t[0], tuple(t[1:]))
def __call__(self, r):
r.hooks['response'] = self.handle_401
return r

View File

@@ -11,7 +11,7 @@ Configurations:
:base_headers: Default HTTP headers.
:verbose: Stream to write request logging to.
:timeout: Seconds until request timeout.
:max_redirects: Maximum njumber of redirects allowed within a request.
:max_redirects: Maximum number of redirects allowed within a request.
:decode_unicode: Decode unicode responses automatically?
:keep_alive: Reuse HTTP Connections?
:max_retries: The number of times a request should be retried in the event of a connection failure.

View File

@@ -13,10 +13,10 @@ class RequestException(Exception):
request."""
class HTTPError(RequestException):
"""An HTTP error occured."""
"""An HTTP error occurred."""
class ConnectionError(RequestException):
"""A Connection error occured."""
"""A Connection error occurred."""
class Timeout(RequestException):
"""The request timed out."""

View File

@@ -13,11 +13,11 @@ import zlib
from urlparse import urlparse, urlunparse, urljoin, urlsplit
from datetime import datetime
from .auth import dispatch as auth_dispatch
from .hooks import dispatch_hook
from .structures import CaseInsensitiveDict
from .status_codes import codes
from .packages import oreos
from .auth import HTTPBasicAuth
from .packages.urllib3.exceptions import MaxRetryError
from .packages.urllib3.exceptions import SSLError as _SSLError
from .packages.urllib3.exceptions import HTTPError as _HTTPError
@@ -98,9 +98,8 @@ class Request(object):
#: content and metadata of HTTP Response, once :attr:`sent <send>`.
self.response = Response()
#: Authentication tuple to attach to :class:`Request <Request>`.
self._auth = auth
self.auth = auth_dispatch(auth)
#: Authentication tuple or object to attach to :class:`Request <Request>`.
self.auth = auth
#: CookieJar to attach to :class:`Request <Request>`.
self.cookies = dict(cookies or [])
@@ -152,7 +151,7 @@ class Request(object):
if resp:
# Fallback to None if there's no staus_code, for whatever reason.
# Fallback to None if there's no status_code, for whatever reason.
response.status_code = getattr(resp, 'status', None)
# Make headers case-insensitive.
@@ -172,7 +171,7 @@ class Request(object):
# Save cookies in Response.
response.cookies = cookies
# Save original resopnse for later.
# Save original response for later.
response.raw = resp
if is_error:
@@ -231,7 +230,7 @@ class Request(object):
files=self.files,
method=method,
params=self.session.params,
auth=self._auth,
auth=self.auth,
cookies=cookies,
redirect=True,
config=self.config,
@@ -344,15 +343,15 @@ class Request(object):
already been sent.
"""
# Build the URL
url = self.full_url
# Logging
if self.config.get('verbose'):
self.config.get('verbose').write('%s %s %s\n' % (
datetime.now().isoformat(), self.method, self.url
datetime.now().isoformat(), self.method, url
))
# Build the URL
url = self.full_url
# Nottin' on you.
body = None
content_type = None
@@ -392,12 +391,13 @@ class Request(object):
if (content_type) and (not 'content-type' in self.headers):
self.headers['Content-Type'] = content_type
if self.auth:
auth_func, auth_args = self.auth
if isinstance(self.auth, tuple) and len(self.auth) == 2:
# special-case basic HTTP auth
self.auth = HTTPBasicAuth(*self.auth)
# Allow auth to make its changes.
r = auth_func(self, *auth_args)
r = self.auth(self)
# Update self to reflect the auth changes.
self.__dict__.update(r.__dict__)
@@ -441,11 +441,12 @@ class Request(object):
headers=self.headers,
redirect=False,
assert_same_host=False,
preload_content=prefetch,
preload_content=False,
decode_content=False,
retries=self.config.get('max_retries', 0),
timeout=self.timeout,
)
self.sent = True
except MaxRetryError, e:
@@ -469,7 +470,8 @@ class Request(object):
# If prefetch is True, mark content as consumed.
if prefetch:
self.response._content_consumed = True
# Save the response.
self.response.content
return self.sent

View File

@@ -123,7 +123,7 @@ class Session(object):
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
:param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.
:param auth: (optional) Auth typle to enable Basic/Digest/Custom HTTP Auth.
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom 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.

View File

@@ -4,7 +4,7 @@
requests.utils
~~~~~~~~~~~~~~
This module provides utlity functions that are used within Requests
This module provides utility functions that are used within Requests
that are also useful for external consumption.
"""
@@ -374,7 +374,7 @@ def requote_path(path):
"""Re-quote the given URL path component.
This function passes the given path through an unquote/quote cycle to
ensure that it is fully and consistenty quoted.
ensure that it is fully and consistently quoted.
"""
parts = path.split("/")
parts = (urllib.quote(urllib.unquote(part), safe="") for part in parts)

View File

@@ -12,12 +12,12 @@ except ImportError:
if sys.argv[-1] == "publish":
os.system("python setup.py sdist upload")
if sys.argv[-1] == 'publish':
os.system('python setup.py sdist upload')
sys.exit()
if sys.argv[-1] == "test":
os.system("python test_requests.py")
if sys.argv[-1] == 'test':
os.system('python test_requests.py')
sys.exit()
required = []
@@ -34,11 +34,14 @@ setup(
author='Kenneth Reitz',
author_email='me@kennethreitz.com',
url='http://python-requests.org',
packages= [
packages=[
'requests',
'requests.packages',
'requests.packages.urllib3'
'requests.packages.urllib3',
'requests.packages.oreos'
],
package_data={'': ['LICENSE', 'NOTICE']},
include_package_data=True,
install_requires=required,
license='ISC',
classifiers=(

View File

@@ -10,6 +10,7 @@ import unittest
import requests
import envoy
from requests import HTTPError
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
try:
import omnijson as json
@@ -80,6 +81,11 @@ class RequestsTestSuite(unittest.TestCase):
r = requests.get(httpbin('/get'))
self.assertEqual(r.status_code, 200)
def test_response_sent(self):
r = requests.get(httpbin('/get'))
self.assertTrue(r.request.sent)
def test_HTTP_302_ALLOW_REDIRECT_GET(self):
r = requests.get(httpbin('redirect', '1'))
self.assertEqual(r.status_code, 200)
@@ -140,7 +146,7 @@ class RequestsTestSuite(unittest.TestCase):
self.assertEqual(r.status_code, 200)
def test_BASICAUTH_HTTP_200_OK_GET(self):
def test_BASICAUTH_TUPLE_HTTP_200_OK_GET(self):
for service in SERVICES:
@@ -159,11 +165,34 @@ class RequestsTestSuite(unittest.TestCase):
self.assertEqual(r.status_code, 200)
def test_BASICAUTH_HTTP_200_OK_GET(self):
for service in SERVICES:
auth = HTTPBasicAuth('user', 'pass')
url = service('basic-auth', 'user', 'pass')
r = requests.get(url, auth=auth)
self.assertEqual(r.status_code, 200)
auth = ('user', 'pass')
r = requests.get(url, auth=auth)
self.assertEqual(r.status_code, 200)
r = requests.get(url)
self.assertEqual(r.status_code, 401)
s = requests.session(auth=auth)
r = s.get(url)
self.assertEqual(r.status_code, 200)
def test_DIGESTAUTH_HTTP_200_OK_GET(self):
for service in SERVICES:
auth = ('digest', 'user', 'pass')
auth = HTTPDigestAuth('user', 'pass')
url = service('digest-auth', 'auth', 'user', 'pass')
r = requests.get(url, auth=auth)
@@ -270,7 +299,7 @@ class RequestsTestSuite(unittest.TestCase):
def test_httpauth_recursion(self):
http_auth = ('user', 'BADpass')
http_auth = HTTPBasicAuth('user', 'BADpass')
for service in SERVICES:
r = requests.get(service('basic-auth', 'user', 'pass'), auth=http_auth)
@@ -553,6 +582,14 @@ class RequestsTestSuite(unittest.TestCase):
r = requests.get(hah, allow_redirects=False, config=config)
assert r.content == None
def test_cached_response(self):
r1 = requests.get(httpbin('get'), prefetch=False)
assert r1.content
assert r1.content
r2 = requests.get(httpbin('get'), prefetch=True)
assert r2.content
if __name__ == '__main__':
unittest.main()