Skip to content
Myr

Merge pull request #3513 from nateprewitt/doc_updates

Before adoption: this commit came in with the history the project adopted, so no landing records it.

doc updates

authored by Kenneth Reitz · committed by GitHub · parents f12166a, 1435cf5 · Browse files · 6 files+45 added−57 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+45 added−57 removed

  • other:+45 added−57 removed

6 files+45 added−57 removed

modified:docs/community/faq.rst+2 added-4 removed
54 unmodified lines
55supported:5657* Python 2.658* Python 2.759* Python 3.160* Python 3.259* Python 3.360* Python 3.463* PyPy 1.964* PyPy 2.261* Python 3.562* PyPy6364What are "hostname doesn't match" errors?65-----------------------------------------66
20 unmodified lines
modified:docs/community/out-there.rst-11 removed
1Integrations2============34ScraperWiki5------------67`ScraperWiki <https://scraperwiki.com/>`_ is an excellent service that allows8you to run Python, Ruby, and PHP scraper scripts on the web. Now, Requests9v0.6.1 is available to use in your scrapers!1011To give it a try, simply::1213 import requests144Python for iOS5--------------67Requests is built into the wonderful `Python for iOS <https://itunes.apple.com/us/app/python-2.7-for-ios/id485729872?mt=Python8>`_ runtime!
15 unmodified lines
modified:docs/dev/todo.rst+1 added-1 removed
39 unmodified lines
40- Python 2.741- Python 3.342- Python 3.443- Python 3.544- PyPy 1.944- PyPy4546Google AppEngine is not officially supported although support is available47with the `Requests-Toolbelt`_.48
12 unmodified lines
modified:docs/user/advanced.rst+29 added-31 removed
85 unmodified lines
8687Request and Response Objects88----------------------------8990Whenever a call is made to ``requests.get()`` and friends you are doing two90Whenever a call is made to ``requests.get()`` and friends, you are doing two91major things. First, you are constructing a ``Request`` object which will be92sent off to a server to request or query some resource. Second, a ``Response``93object is generated once ``requests`` gets a response back from the server.94The Response object contains all of the information returned by the server and93object is generated once Requests gets a response back from the server.94The ``Response`` object contains all of the information returned by the server and95also contains the ``Request`` object you created originally. Here is a simple96request to get some very important information from Wikipedia's servers::9798 >>> r = requests.get('http://en.wikipedia.org/wiki/Monty_Python')
113 unmodified lines
212 the c_rehash utility supplied with OpenSSL.213214This list of trusted CAs can also be specified through the ``REQUESTS_CA_BUNDLE`` environment variable.215216Requests can also ignore verifying the SSL certificate if you set ``verify`` to False.216Requests can also ignore verifying the SSL certificate if you set ``verify`` to False::217218::219218 >>> requests.get('https://kennethreitz.com', verify=False)219 <Response [200]>220221By default, ``verify`` is set to True. Option ``verify`` only applies to host certs.
17 unmodified lines
239240CA Certificates241---------------242245By default Requests bundles a set of root CAs that it trusts, sourced from the243By default, Requests bundles a set of root CAs that it trusts, sourced from the244`Mozilla trust store`_. However, these are only updated once for each Requests245version. This means that if you pin a Requests version your certificates can246become extremely out of date.247
15 unmodified lines
263---------------------264265By default, when you make a request, the body of the response is downloaded266immediately. You can override this behaviour and defer downloading the response269body until you access the :class:`Response.content <requests.Response.content>`267body until you access the :attr:`Response.content <requests.Response.content>`268attribute with the ``stream`` parameter::269270 tarball_url = 'https://github.com/kennethreitz/requests/tarball/master'271 r = requests.get(tarball_url, stream=True)
4 unmodified lines
276 if int(r.headers['content-length']) < TOO_LONG:277 content = r.content278 ...279282You can further control the workflow by use of the :class:`Response.iter_content <requests.Response.iter_content>`283and :class:`Response.iter_lines <requests.Response.iter_lines>` methods.280You can further control the workflow by use of the :meth:`Response.iter_content() <requests.Response.iter_content>`281and :meth:`Response.iter_lines() <requests.Response.iter_lines>` methods.282Alternatively, you can read the undecoded body from the underlying283urllib3 :class:`urllib3.HTTPResponse <urllib3.response.HTTPResponse>` at286:class:`Response.raw <requests.Response.raw>`.284:attr:`Response.raw <requests.Response.raw>`.285286If you set ``stream`` to ``True`` when making a request, Requests cannot287release the connection back to the pool unless you consume all the data or call290:class:`Response.close <requests.Response.close>`. This can lead to288:meth:`Response.close <requests.Response.close>`. This can lead to289inefficiency with connections. If you find yourself partially reading request290bodies (or not reading them at all) while using ``stream=True``, you should291consider using ``contextlib.closing`` (`documented here`_), like this::292
53 unmodified lines
346347 requests.post('http://some.url/chunked', data=gen())348349For chunked encoded responses, it's best to iterate over the data using352:meth:`Response.iter_content() <requests.models.Response.iter_content>`. In350:meth:`Response.iter_content() <requests.Response.iter_content>`. In351an ideal situation you'll have set ``stream=True`` on the request, in which354case you can iterate chunk-by-chunk by calling ``iter_content`` with a chunk355size parameter of ``None``. If you want to set a maximum size of the chunk,356you can set a chunk size parameter to any integer.352case you can iterate chunk-by-chunk by calling ``iter_content`` with a ``chunk_size``353parameter of ``None``. If you want to set a maximum size of the chunk,354you can set a ``chunk_size`` parameter to any integer.355356357.. _multipart:358
78 unmodified lines
437438Any callable which is passed as the ``auth`` argument to a request method will439have the opportunity to modify the request before it is dispatched.440443Authentication implementations are subclasses of ``requests.auth.AuthBase``,441Authentication implementations are subclasses of :class:`AuthBase <requests.auth.AuthBase>`,442and are easy to define. Requests provides two common authentication scheme445implementations in ``requests.auth``: ``HTTPBasicAuth`` and ``HTTPDigestAuth``.443implementations in ``requests.auth``: :class:`HTTPBasicAuth <requests.auth.HTTPBasicAuth>` and444:class:`HTTPDigestAuth <requests.auth.HTTPDigestAuth>`.445446Let's pretend that we have a web service that will only respond if the447``X-Pizza`` header is set to a password value. Unlikely, but just go with it.448
21 unmodified lines
470471Streaming Requests472------------------473475With :class:`requests.Response.iter_lines()` you can easily474With :meth:`Response.iter_lines() <requests.Response.iter_lines>` you can easily475iterate over streaming APIs such as the `Twitter Streaming476API <https://dev.twitter.com/streaming/overview>`_. Simply477set ``stream`` to ``True`` and iterate over the response with479:class:`~requests.Response.iter_lines()`::478:meth:`~requests.Response.iter_lines()`::479480 import json481 import requests482
6 unmodified lines
489 print(json.loads(line))490491.. warning::492494 :class:`~requests.Response.iter_lines()` is not reentrant safe.493 :meth:`~requests.Response.iter_lines()` is not reentrant safe.494 Calling this method multiple times causes some of the received data495 being lost. In case you need to call it from multiple places, use496 the resulting iterator object instead::497
169 unmodified lines
667commits is POST, which creates a new commit. As we're using the Requests repo,668we should probably avoid making ham-handed POSTS to it. Instead, let's play669with the Issues feature of GitHub.670672This documentation was added in response to Issue #482. Given that this issue673already exists, we will use it as an example. Let's start by getting it.671This documentation was added in response to672`Issue #482 <https://github.com/kennethreitz/requests/issues/482>`_. Given that673this issue already exists, we will use it as an example. Let's start by getting it.674675::676677 >>> r = requests.get('https://api.github.com/repos/kennethreitz/requests/issues/482')
166 unmodified lines
844with the given prefix will use the given Transport Adapter.845846Many of the details of implementing a Transport Adapter are beyond the scope of847this documentation, but take a look at the next example for a simple SSL use-848case. For more than that, you might look at subclassing849``requests.adapters.BaseAdapter``.848case. For more than that, you might look at subclassing the849:class:`BaseAdapter <requests.adapters.BaseAdapter>`.850851Example: Specific SSL Version852^^^^^^^^^^^^^^^^^^^^^^^^^^^^^853
3 unmodified lines
857that uses a version that isn't compatible with the default.858859You can use Transport Adapters for this by taking most of the existing860implementation of HTTPAdapter, and adding a parameter *ssl_version* that gets861passed-through to `urllib3`. We'll make a TA that instructs the library to use862SSLv3:861passed-through to `urllib3`. We'll make a Transport Adapter that instructs the862library to use SSLv3::863864::865864 import ssl865866 from requests.adapters import HTTPAdapter867 from requests.packages.urllib3.poolmanager import PoolManager
33 unmodified lines
901---------------902903In unusual circumstances you may want to provide headers in an ordered manner. If you pass an ``OrderedDict`` to the ``headers`` keyword argument, that will provide the headers with an ordering. *However*, the ordering of the default headers used by Requests will be preferred, which means that if you override default headers in the ``headers`` keyword argument, they may appear out of order compared to other headers in that keyword argument.904907If this is problematic, users should consider setting the default headers on a :class:`Session <requests.Session>` object, by setting :data:`Session <requests.Session.headers>` to a custom ``OrderedDict``. That ordering will always be preferred.905If this is problematic, users should consider setting the default headers on a :class:`Session <requests.Session>` object, by setting :attr:`Session <requests.Session.headers>` to a custom ``OrderedDict``. That ordering will always be preferred.906907.. _timeouts:908909Timeouts
27 unmodified lines
937If the remote server is very slow, you can tell Requests to wait forever for938a response, by passing None as a timeout value and then retrieving a cup of939coffee.940943.. code-block:: python941::942943 r = requests.get('https://github.com', timeout=None)944945.. _`connect()`: http://linux.die.net/man/2/connect
modified:docs/user/quickstart.rst+10 added-10 removed
25 unmodified lines
2627 >>> import requests2829Now, let's try to get a webpage. For this example, let's get GitHub's public30timeline ::30timeline::3132 >>> r = requests.get('https://api.github.com/events')3334Now, we have a :class:`Response <requests.Response>` object called ``r``. We can
112 unmodified lines
147 >>> r = requests.get('https://api.github.com/events')148 >>> r.json()149 [{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...150151In case the JSON decoding fails, ``r.json`` raises an exception. For example, if151In case the JSON decoding fails, ``r.json()`` raises an exception. For example, if152the response gets a 204 (No Content), or if the response contains invalid JSON,153attempting ``r.json`` raises ``ValueError: No JSON object could be decoded``.153attempting ``r.json()`` raises ``ValueError: No JSON object could be decoded``.154155It should be noted that the success of the call to ``r.json`` does **not**155It should be noted that the success of the call to ``r.json()`` does **not**156indicate the success of the response. Some servers may return a JSON object in a157failed response (e.g. error details with HTTP 500). Such JSON will be decoded158and returned. To check that a request is successful, use159``r.raise_for_status()`` or check ``r.status_code`` is what you expect.
278 unmodified lines
438HEAD.439440We can use the ``history`` property of the Response object to track redirection.441442The :meth:`Response.history <requests.Response.history>` list contains the442The :attr:`Response.history <requests.Response.history>` list contains the443:class:`Response <requests.Response>` objects that were created in order to444complete the request. The list is sorted from the oldest to the most recent445response.446
56 unmodified lines
503Errors and Exceptions504---------------------505506In the event of a network problem (e.g. DNS failure, refused connection, etc),507Requests will raise a :class:`~requests.exceptions.ConnectionError` exception.507Requests will raise a :exc:`~requests.exceptions.ConnectionError` exception.508509:meth:`Response.raise_for_status() <requests.Response.raise_for_status>` will510raise an :class:`~requests.exceptions.HTTPError` if the HTTP request510raise an :exc:`~requests.exceptions.HTTPError` if the HTTP request511returned an unsuccessful status code.512513If a request times out, a :class:`~requests.exceptions.Timeout` exception is513If a request times out, a :exc:`~requests.exceptions.Timeout` exception is514raised.515516If a request exceeds the configured number of maximum redirections, a517:class:`~requests.exceptions.TooManyRedirects` exception is raised.517:exc:`~requests.exceptions.TooManyRedirects` exception is raised.518519All exceptions that Requests explicitly raises inherit from520:class:`requests.exceptions.RequestException`.520:exc:`requests.exceptions.RequestException`.521522-----------------------523524Ready for more? Check out the :ref:`advanced <advanced>` section.
modified:docs/api.rst+3 added
61 unmodified lines
6263.. autoclass:: requests.PreparedRequest64 :inherited-members:6566.. autoclass:: requests.adapters.BaseAdapter67 :inherited-members:6869.. autoclass:: requests.adapters.HTTPAdapter70 :inherited-members:7172Authentication
198 unmodified lines