quickstart.rst
This file
Path
docs/user/quickstart.rst
495 lines · 14.0 KiB
Last changed by
Merge remote-tracking branch 'origin/master'Kenneth Reitzbefore adoption7e47d5e
History of this fileTake it
The walk stopped after 200 commits: lines older than 0a37d82 point at it.
Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 1–17why, one step back1.. _quickstart:23Quickstart4==========56.. module:: requests.models78Eager to get started? This page gives a good introduction in how to get started9with Requests. This assumes you already have Requests installed. If you do not,10head over to the :ref:`Installation <install>` section.1112First, make sure that:1314* Requests is :ref:`installed <install>`15* Requests is :ref:`up-to-date <updates>`1617Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 18why, one step back18Let's get started with some simple examples.Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 19–20why, one step back1920Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 21why, one step back21Make a RequestMerge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 22–23why, one step back22------------------23Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 24why, one step back24Making a request with Requests is very simple.Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 25why, one step back25Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 26why, one step back26Begin by importing the Requests module::Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8
line 27why, one step back27Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 28why, one step back28 >>> import requestsMerge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 29why, one step back29Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 30–31why, one step back30Now, let's try to get a webpage. For this example, let's get GitHub's public31timeline ::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 32why, one step back32Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 33–34why, one step back33 >>> r = requests.get('https://github.com/timeline.json')34Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 35why, one step back35Now, we have a :class:`Response` object called ``r``. We can get all theMerge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 36why, one step back36information we need from this object.Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 37why, one step back37Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 38–39why, one step back38Requests' simple API means that all forms of HTTP request are as obvious. For39example, this is how you make an HTTP POST request::Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8
line 40why, one step back40Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 41why, one step back41 >>> r = requests.post("http://httpbin.org/post")Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 42why, one step back42Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 43–44why, one step back43Nice, right? What about the other HTTP request types: PUT, DELETE, HEAD and44OPTIONS? These are all just as simple::Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8
line 45why, one step back45Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 46–65why, one step back46 >>> r = requests.put("http://httpbin.org/put")47 >>> r = requests.delete("http://httpbin.org/delete")48 >>> r = requests.head("http://httpbin.org/get")49 >>> r = requests.options("http://httpbin.org/get")5051That's all well and good, but it's also only the start of what Requests can52do.535455Passing Parameters In URLs56--------------------------5758You often want to send some sort of data in the URL's query string. If59you were constructing the URL by hand, this data would be given as key/value60pairs in the URL after a question mark, e.g. ``httpbin.org/get?key=val``.61Requests allows you to provide these arguments as a dictionary, using the62``params`` keyword argument. As an example, if you wanted to pass63``key1=value1`` and ``key2=value2`` to ``httpbin.org/get``, you would use the64following code::65Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 66–68why, one step back66 >>> payload = {'key1': 'value1', 'key2': 'value2'}67 >>> r = requests.get("http://httpbin.org/get", params=payload)68Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 69why, one step back69You can see that the URL has been correctly encoded by printing the URL::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 70why, one step back70Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 71–72why, one step back71 >>> print r.url72 u'http://httpbin.org/get?key2=value2&key1=value1'Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 73why, one step back73Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8
line 74why, one step back74Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 75–77why, one step back75Response Content76----------------77Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 78–79why, one step back78We can read the content of the server's response. Consider the GitHub timeline79again::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 80why, one step back80Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 81–82why, one step back81 >>> import requests82 >>> r = requests.get('https://github.com/timeline.json')Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 83–88why, one step back83 >>> r.text84 '[{"repository":{"open_issues":0,"url":"https://github.com/...8586Requests will automatically decode content from the server. Most unicode87charsets are seamlessly decoded.88Merge pull request #772 from Lukasa/developKenneth Reitzbefore adoption6464618
lines 89–92why, one step back89When you make a request, Requests makes educated guesses about the encoding of90the response based on the HTTP headers. The text encoding guessed by Requests91is used when you access ``r.text``. You can find out what encoding Requests is92using, and change it, using the ``r.encoding`` property::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 93why, one step back93Merge pull request #772 from Lukasa/developKenneth Reitzbefore adoption6464618
lines 94–104why, one step back94 >>> r.encoding95 'utf-8'96 >>> r.encoding = 'ISO-8859-1'9798If you change the encoding, Requests will use the new value of ``r.encoding``99whenever you call ``r.text``.100101Requests will also use custom encodings in the event that you need them. If102you have created your own encoding and registered it with the ``codecs``103module, you can simply use the codec name as the value of ``r.encoding`` and104Requests will handle the decoding for you.Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 105–115why, one step back105106Binary Response Content107-----------------------108109You can also access the response body as bytes, for non-text requests::110111 >>> r.content112 b'[{"repository":{"open_issues":0,"url":"https://github.com/...113114The ``gzip`` and ``deflate`` transfer-encodings are automatically decoded for you.115Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 116why, one step back116For example, to create an image from binary data returned by a request, you canMerge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 117–123why, one step back117use the following code:118119 >>> from PIL import Image120 >>> from StringIO import StringIO121 >>> i = Image.open(StringIO(r.content))122123Added r.json documentation (document ALL the awesome features!)Danilo Bargenbefore adoption1164450
lines 124–136why, one step back124JSON Response Content125---------------------126127There's also a builtin JSON decoder, in case you're dealing with JSON data::128129 >>> import requests130 >>> r = requests.get('https://github.com/timeline.json')131 >>> r.json132 [{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...133134In case the JSON decoding fails, ``r.json`` simply returns ``None``.135136Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 137–139why, one step back137Raw Response Content138--------------------139Merge remote-tracking branch 'origin/master'Kenneth Reitzbefore adoption7e47d5e
lines 140–142why, one step back140In the rare case that you'd like to get the raw socket response from the141server, you can access ``r.raw``. If you want to do this, make sure you set142``prefetch=False`` in your initial request. Once you do, you can do this::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 143why, one step back143Merge remote-tracking branch 'origin/master'Kenneth Reitzbefore adoption7e47d5e
line 144why, one step back144 >>> r = requests.get('https:/github.com/timeline.json', prefetch=False)Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 145–150why, one step back145 >>> r.raw146 <requests.packages.urllib3.response.HTTPResponse object at 0x101194810>147 >>> r.raw.read(10)148 '\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'149150Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 151–152why, one step back151Custom Headers152--------------Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 153why, one step back153Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 154–155why, one step back154If you'd like to add HTTP headers to a request, simply pass in a ``dict`` to the155``headers`` parameter.Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 156why, one step back156Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 157why, one step back157For example, we didn't specify our content-type in the previous example::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 158why, one step back158Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 159–162why, one step back159 >>> import json160 >>> url = 'https://api.github.com/some/endpoint'161 >>> payload = {'some': 'data'}162 >>> headers = {'content-type': 'application/json'}Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 163why, one step back163Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 164why, one step back164 >>> r = requests.post(url, data=json.dumps(payload), headers=headers)Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 165why, one step back165Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 166–169why, one step back166167More complicated POST requests168------------------------------169Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 170–177why, one step back170Typically, you want to send some form-encoded data — much like an HTML form.171To do this, simply pass a dictionary to the `data` argument. Your172dictionary of data will automatically be form-encoded when the request is made::173174 >>> payload = {'key1': 'value1', 'key2': 'value2'}175 >>> r = requests.post("http://httpbin.org/post", data=payload)176 >>> print r.text177 {Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 178why, one step back178 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 179–182why, one step back179 "form": {180 "key2": "value2",181 "key1": "value1"182 },Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 183why, one step back183 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 184–189why, one step back184 }185186There are many times that you want to send data that is not form-encoded. If you pass in a ``string`` instead of a ``dict``, that data will be posted directly.187188For example, the GitHub API v3 accepts JSON-Encoded POST/PATCH data::189Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 190–192why, one step back190 >>> import json191 >>> url = 'https://api.github.com/some/endpoint'192 >>> payload = {'some': 'data'}Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 193why, one step back193Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 194why, one step back194 >>> r = requests.post(url, data=json.dumps(payload))Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 195–202why, one step back195196197POST a Multipart-Encoded File198-----------------------------199200Requests makes it simple to upload Multipart-encoded files::201202 >>> url = 'http://httpbin.org/post'Merge pull request #752 from anentropic/patch-1Kenneth Reitzbefore adoption54842cd
line 203why, one step back203 >>> files = {'file': open('report.xls', 'rb')}Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 204–207why, one step back204205 >>> r = requests.post(url, files=files)206 >>> r.text207 {Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 208why, one step back208 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 209why, one step back209 "files": {Merge pull request #752 from anentropic/patch-1Kenneth Reitzbefore adoption54842cd
line 210why, one step back210 "file": "<censored...binary...data>"Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 211why, one step back211 },Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 212why, one step back212 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 213–214why, one step back213 }214Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 215why, one step back215You can set the filename explicitly::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 216–222why, one step back216217 >>> url = 'http://httpbin.org/post'218 >>> files = {'file': ('report.xls', open('report.xls', 'rb'))}219220 >>> r = requests.post(url, files=files)221 >>> r.text222 {Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 223why, one step back223 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 224–226why, one step back224 "files": {225 "file": "<censored...binary...data>"226 },Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 227why, one step back227 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 228–229why, one step back228 }229Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 230why, one step back230If you want, you can send strings to be received as files::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 231–232why, one step back231232 >>> url = 'http://httpbin.org/post'Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8
line 233why, one step back233 >>> files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 234–237why, one step back234235 >>> r = requests.post(url, files=files)236 >>> r.text237 {Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 238why, one step back238 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 239–241why, one step back239 "files": {240 "file": "some,data,to,send\\nanother,row,to,send\\n"241 },Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 242why, one step back242 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 243–250why, one step back243 }244245246Response Status Codes247---------------------248249We can check the response status code::250Merge remote-tracking branch 'origin/develop' into developKenneth Reitzbefore adoption6541ceb
line 251why, one step back251 >>> r = requests.get('http://httpbin.org/get')Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 252–263why, one step back252 >>> r.status_code253 200254255Requests also comes with a built-in status code lookup object for easy256reference::257258 >>> r.status_code == requests.codes.ok259 True260261If we made a bad request (non-200 response), we can raise it with262:class:`Response.raise_for_status()`::263Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8
lines 264–265why, one step back264 >>> bad_r = requests.get('http://httpbin.org/status/404')265 >>> bad_r.status_codeMerge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 266–267why, one step back266 404267Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8
line 268why, one step back268 >>> bad_r.raise_for_status()Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 269why, one step back269 Traceback (most recent call last):Merge pull request #683 from jkleint/patch-1Kenneth Reitzbefore adoption38db8c2
lines 270–272why, one step back270 File "requests/models.py", line 832, in raise_for_status271 raise http_error272 requests.exceptions.HTTPError: 404 Client ErrorMerge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 273why, one step back273Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 274–275why, one step back274But, since our ``status_code`` for ``r`` was ``200``, when we call275``raise_for_status()`` we get::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 276–285why, one step back276277 >>> r.raise_for_status()278 None279280All is well.281282283Response Headers284----------------285Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 286why, one step back286We can view the server's response headers using a Python dictionary::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 287–342why, one step back287288 >>> r.headers289 {290 'status': '200 OK',291 'content-encoding': 'gzip',292 'transfer-encoding': 'chunked',293 'connection': 'close',294 'server': 'nginx/1.0.4',295 'x-runtime': '148ms',296 'etag': '"e1ca502697e5c9317743dc078f67693f"',297 'content-type': 'application/json; charset=utf-8'298 }299300The dictionary is special, though: it's made just for HTTP headers. According to301`RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>`_, HTTP302Headers are case-insensitive.303304So, we can access the headers using any capitalization we want::305306 >>> r.headers['Content-Type']307 'application/json; charset=utf-8'308309 >>> r.headers.get('content-type')310 'application/json; charset=utf-8'311312If a header doesn't exist in the Response, its value defaults to ``None``::313314 >>> r.headers['X-Random']315 None316317318Cookies319-------320321If a response contains some Cookies, you can get quick access to them::322323 >>> url = 'http://httpbin.org/cookies/set/requests-is/awesome'324 >>> r = requests.get(url)325326 >>> r.cookies['requests-is']327 'awesome'328329To send your own cookies to the server, you can use the ``cookies``330parameter::331332 >>> url = 'http://httpbin.org/cookies'333 >>> cookies = dict(cookies_are='working')334335 >>> r = requests.get(url, cookies=cookies)336 >>> r.text337 '{"cookies": {"cookies_are": "working"}}'338339340Basic Authentication341--------------------342Merge pull request #787 from kayzh/developKenneth Reitzbefore adoptionbbf1946
line 343why, one step back343Many web services require authentication. There are many different types ofMerge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 344–375why, one step back344authentication, but the most common is HTTP Basic Auth.345346Making requests with Basic Auth is extremely simple::347348 >>> from requests.auth import HTTPBasicAuth349 >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))350 <Response [200]>351352Due to the prevalence of HTTP Basic Auth, requests provides a shorthand for353this authentication method::354355 >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))356 <Response [200]>357358Providing the credentials as a tuple in this fashion is functionally equivalent359to the ``HTTPBasicAuth`` example above.360361362Digest Authentication363---------------------364365Another popular form of web service protection is Digest Authentication::366367 >>> from requests.auth import HTTPDigestAuth368 >>> url = 'http://httpbin.org/digest-auth/auth/user/pass'369 >>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))370 <Response [200]>371372373OAuth Authentication374--------------------375oauth awesomenessKenneth Reitzbefore adoptionf33a0aa
lines 376–377why, one step back376Requests features robust, built-in OAuth support!377typo all the thingsKenneth Reitzbefore adoption9840966
line 378why, one step back378OAuth takes many forms, so let's take a look at a few different forms::oauth awesomenessKenneth Reitzbefore adoptionf33a0aa
lines 379–411why, one step back379380 import requests381 from requests.auth import OAuth1382383 url = u'https://api.twitter.com/1/account/settings.json'384385 client_key = u'...'386 client_secret = u'...'387 resource_owner_key = u'...'388 resource_owner_secret = u'...'389390391Query signing::392393 queryoauth = OAuth1(client_key, client_secret,394 resource_owner_key, resource_owner_secret,395 signature_type='query')396 r = requests.get(url, auth=queryoauth)397398Header signing::399400 headeroauth = OAuth1(client_key, client_secret,401 resource_owner_key, resource_owner_secret,402 signature_type='auth_header')403 r = requests.get(url, auth=headeroauth)404405Body signing::406407 bodyoauth = OAuth1(client_key, client_secret,408 resource_owner_key, resource_owner_secret,409 signature_type='body')410411 r = requests.post(url, auth=bodyoauth)Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 412–416why, one step back412413414Redirection and History415-----------------------416Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 417–418why, one step back417Requests will automatically perform location redirection while using the GET418and OPTIONS verbs.Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 419why, one step back419Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 420–421why, one step back420GitHub redirects all HTTP requests to HTTPS. We can use the ``history`` method421of the Response object to track redirection. Let's see what Github does::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 422–431why, one step back422423 >>> r = requests.get('http://github.com')424 >>> r.url425 'https://github.com/'426 >>> r.status_code427 200428 >>> r.history429 [<Response [301]>]430431The :class:`Response.history` list contains a list of theMerge pull request #721 from karlcow/developKenneth Reitzbefore adoptionc3ab38e
line 432why, one step back432:class:`Request` objects that were created in order to complete the request. The list is sorted from the oldest to the most recent request.Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 433why, one step back433Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 434–435why, one step back434If you're using GET or OPTIONS, you can disable redirection handling with the435``allow_redirects`` parameter::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 436–442why, one step back436437 >>> r = requests.get('http://github.com', allow_redirects=False)438 >>> r.status_code439 301440 >>> r.history441 []442Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 443–444why, one step back443If you're using POST, PUT, PATCH, DELETE or HEAD, you can enable444redirection as well::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 445–455why, one step back445446 >>> r = requests.post('http://github.com', allow_redirects=True)447 >>> r.url448 'https://github.com/'449 >>> r.history450 [<Response [301]>]451452453Timeouts454--------455Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 456–457why, one step back456You can tell requests to stop waiting for a response after a given number of457seconds with the ``timeout`` parameter::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 458–463why, one step back458459 >>> requests.get('http://github.com', timeout=0.001)460 Traceback (most recent call last):461 File "<stdin>", line 1, in <module>462 requests.exceptions.Timeout: Request timed out.463Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 464why, one step back464.. admonition:: Note:Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 465why, one step back465Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 466–467why, one step back466 ``timeout`` only effects the connection process itself, not the467 downloading of the response body.Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 468–480why, one step back468469470Errors and Exceptions471---------------------472473In the event of a network problem (e.g. DNS failure, refused connection, etc),474Requests will raise a :class:`ConnectionError` exception.475476In the event of the rare invalid HTTP response, Requests will raise477an :class:`HTTPError` exception.478479If a request times out, a :class:`Timeout` exception is raised.480Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 481–482why, one step back481If a request exceeds the configured number of maximum redirections, a482:class:`TooManyRedirects` exception is raised.Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 483–486why, one step back483484All exceptions that Requests explicitly raises inherit from485:class:`requests.exceptions.RequestException`.486Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 487–491why, one step back487You can refer to :ref:`Configuration API Docs <configurations>` for immediate488raising of :class:`HTTPError` exceptions via the ``danger_mode`` option or489have Requests catch the majority of490:class:`requests.exceptions.RequestException` exceptions with the ``safe_mode``491option.Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 492–495why, one step back492493-----------------------494495Ready for more? Check out the :ref:`advanced <advanced>` section.