quickstart.rst
This file
Path
docs/user/quickstart.rst
494 lines · 13.8 KiB
Last changed by
typo all the thingsKenneth Reitzbefore adoption9840966
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–149why, one step back137Raw Response Content138--------------------139140In the rare case that you'd like to get the absolute raw socket response from the server,141you can access ``r.raw``::142143 >>> r.raw144 <requests.packages.urllib3.response.HTTPResponse object at 0x101194810>145146 >>> r.raw.read(10)147 '\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'148149Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 150–151why, one step back150Custom Headers151--------------Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 152why, one step back152Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 153–154why, one step back153If you'd like to add HTTP headers to a request, simply pass in a ``dict`` to the154``headers`` parameter.Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 155why, one step back155Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 156why, one step back156For example, we didn't specify our content-type in the previous example::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 157why, one step back157Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 158–161why, one step back158 >>> import json159 >>> url = 'https://api.github.com/some/endpoint'160 >>> payload = {'some': 'data'}161 >>> headers = {'content-type': 'application/json'}Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 162why, one step back162Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 163why, one step back163 >>> r = requests.post(url, data=json.dumps(payload), headers=headers)Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 164why, one step back164Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 165–168why, one step back165166More complicated POST requests167------------------------------168Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 169–176why, one step back169Typically, you want to send some form-encoded data — much like an HTML form.170To do this, simply pass a dictionary to the `data` argument. Your171dictionary of data will automatically be form-encoded when the request is made::172173 >>> payload = {'key1': 'value1', 'key2': 'value2'}174 >>> r = requests.post("http://httpbin.org/post", data=payload)175 >>> print r.text176 {Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 177why, one step back177 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 178–181why, one step back178 "form": {179 "key2": "value2",180 "key1": "value1"181 },Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 182why, one step back182 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 183–188why, one step back183 }184185There 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.186187For example, the GitHub API v3 accepts JSON-Encoded POST/PATCH data::188Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 189–191why, one step back189 >>> import json190 >>> url = 'https://api.github.com/some/endpoint'191 >>> payload = {'some': 'data'}Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 192why, one step back192Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 193why, one step back193 >>> r = requests.post(url, data=json.dumps(payload))Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 194–201why, one step back194195196POST a Multipart-Encoded File197-----------------------------198199Requests makes it simple to upload Multipart-encoded files::200201 >>> url = 'http://httpbin.org/post'Merge pull request #752 from anentropic/patch-1Kenneth Reitzbefore adoption54842cd
line 202why, one step back202 >>> files = {'file': open('report.xls', 'rb')}Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 203–206why, one step back203204 >>> r = requests.post(url, files=files)205 >>> r.text206 {Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 207why, one step back207 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 208why, one step back208 "files": {Merge pull request #752 from anentropic/patch-1Kenneth Reitzbefore adoption54842cd
line 209why, one step back209 "file": "<censored...binary...data>"Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 210why, one step back210 },Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 211why, one step back211 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 212–213why, one step back212 }213Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 214why, one step back214You can set the filename explicitly::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 215–221why, one step back215216 >>> url = 'http://httpbin.org/post'217 >>> files = {'file': ('report.xls', open('report.xls', 'rb'))}218219 >>> r = requests.post(url, files=files)220 >>> r.text221 {Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 222why, one step back222 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 223–225why, one step back223 "files": {224 "file": "<censored...binary...data>"225 },Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 226why, one step back226 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 227–228why, one step back227 }228Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 229why, one step back229If you want, you can send strings to be received as files::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 230–231why, one step back230231 >>> url = 'http://httpbin.org/post'Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8
line 232why, one step back232 >>> files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 233–236why, one step back233234 >>> r = requests.post(url, files=files)235 >>> r.text236 {Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 237why, one step back237 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 238–240why, one step back238 "files": {239 "file": "some,data,to,send\\nanother,row,to,send\\n"240 },Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 241why, one step back241 // ...snip... //Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 242–249why, one step back242 }243244245Response Status Codes246---------------------247248We can check the response status code::249Merge remote-tracking branch 'origin/develop' into developKenneth Reitzbefore adoption6541ceb
line 250why, one step back250 >>> r = requests.get('http://httpbin.org/get')Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 251–262why, one step back251 >>> r.status_code252 200253254Requests also comes with a built-in status code lookup object for easy255reference::256257 >>> r.status_code == requests.codes.ok258 True259260If we made a bad request (non-200 response), we can raise it with261:class:`Response.raise_for_status()`::262Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8
lines 263–264why, one step back263 >>> bad_r = requests.get('http://httpbin.org/status/404')264 >>> bad_r.status_codeMerge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 265–266why, one step back265 404266Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8
line 267why, one step back267 >>> bad_r.raise_for_status()Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 268why, one step back268 Traceback (most recent call last):Merge pull request #683 from jkleint/patch-1Kenneth Reitzbefore adoption38db8c2
lines 269–271why, one step back269 File "requests/models.py", line 832, in raise_for_status270 raise http_error271 requests.exceptions.HTTPError: 404 Client ErrorMerge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 272why, one step back272Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 273–274why, one step back273But, since our ``status_code`` for ``r`` was ``200``, when we call274``raise_for_status()`` we get::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 275–284why, one step back275276 >>> r.raise_for_status()277 None278279All is well.280281282Response Headers283----------------284Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 285why, one step back285We can view the server's response headers using a Python dictionary::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 286–341why, one step back286287 >>> r.headers288 {289 'status': '200 OK',290 'content-encoding': 'gzip',291 'transfer-encoding': 'chunked',292 'connection': 'close',293 'server': 'nginx/1.0.4',294 'x-runtime': '148ms',295 'etag': '"e1ca502697e5c9317743dc078f67693f"',296 'content-type': 'application/json; charset=utf-8'297 }298299The dictionary is special, though: it's made just for HTTP headers. According to300`RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>`_, HTTP301Headers are case-insensitive.302303So, we can access the headers using any capitalization we want::304305 >>> r.headers['Content-Type']306 'application/json; charset=utf-8'307308 >>> r.headers.get('content-type')309 'application/json; charset=utf-8'310311If a header doesn't exist in the Response, its value defaults to ``None``::312313 >>> r.headers['X-Random']314 None315316317Cookies318-------319320If a response contains some Cookies, you can get quick access to them::321322 >>> url = 'http://httpbin.org/cookies/set/requests-is/awesome'323 >>> r = requests.get(url)324325 >>> r.cookies['requests-is']326 'awesome'327328To send your own cookies to the server, you can use the ``cookies``329parameter::330331 >>> url = 'http://httpbin.org/cookies'332 >>> cookies = dict(cookies_are='working')333334 >>> r = requests.get(url, cookies=cookies)335 >>> r.text336 '{"cookies": {"cookies_are": "working"}}'337338339Basic Authentication340--------------------341Merge pull request #787 from kayzh/developKenneth Reitzbefore adoptionbbf1946
line 342why, one step back342Many web services require authentication. There are many different types ofMerge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 343–374why, one step back343authentication, but the most common is HTTP Basic Auth.344345Making requests with Basic Auth is extremely simple::346347 >>> from requests.auth import HTTPBasicAuth348 >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))349 <Response [200]>350351Due to the prevalence of HTTP Basic Auth, requests provides a shorthand for352this authentication method::353354 >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))355 <Response [200]>356357Providing the credentials as a tuple in this fashion is functionally equivalent358to the ``HTTPBasicAuth`` example above.359360361Digest Authentication362---------------------363364Another popular form of web service protection is Digest Authentication::365366 >>> from requests.auth import HTTPDigestAuth367 >>> url = 'http://httpbin.org/digest-auth/auth/user/pass'368 >>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))369 <Response [200]>370371372OAuth Authentication373--------------------374oauth awesomenessKenneth Reitzbefore adoptionf33a0aa
lines 375–376why, one step back375Requests features robust, built-in OAuth support!376typo all the thingsKenneth Reitzbefore adoption9840966
line 377why, one step back377OAuth takes many forms, so let's take a look at a few different forms::oauth awesomenessKenneth Reitzbefore adoptionf33a0aa
lines 378–410why, one step back378379 import requests380 from requests.auth import OAuth1381382 url = u'https://api.twitter.com/1/account/settings.json'383384 client_key = u'...'385 client_secret = u'...'386 resource_owner_key = u'...'387 resource_owner_secret = u'...'388389390Query signing::391392 queryoauth = OAuth1(client_key, client_secret,393 resource_owner_key, resource_owner_secret,394 signature_type='query')395 r = requests.get(url, auth=queryoauth)396397Header signing::398399 headeroauth = OAuth1(client_key, client_secret,400 resource_owner_key, resource_owner_secret,401 signature_type='auth_header')402 r = requests.get(url, auth=headeroauth)403404Body signing::405406 bodyoauth = OAuth1(client_key, client_secret,407 resource_owner_key, resource_owner_secret,408 signature_type='body')409410 r = requests.post(url, auth=bodyoauth)Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 411–415why, one step back411412413Redirection and History414-----------------------415Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 416–417why, one step back416Requests will automatically perform location redirection while using the GET417and OPTIONS verbs.Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 418why, one step back418Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 419–420why, one step back419GitHub redirects all HTTP requests to HTTPS. We can use the ``history`` method420of the Response object to track redirection. Let's see what Github does::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 421–430why, one step back421422 >>> r = requests.get('http://github.com')423 >>> r.url424 'https://github.com/'425 >>> r.status_code426 200427 >>> r.history428 [<Response [301]>]429430The :class:`Response.history` list contains a list of theMerge pull request #721 from karlcow/developKenneth Reitzbefore adoptionc3ab38e
line 431why, one step back431: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 432why, one step back432Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 433–434why, one step back433If you're using GET or OPTIONS, you can disable redirection handling with the434``allow_redirects`` parameter::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 435–441why, one step back435436 >>> r = requests.get('http://github.com', allow_redirects=False)437 >>> r.status_code438 301439 >>> r.history440 []441Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 442–443why, one step back442If you're using POST, PUT, PATCH, DELETE or HEAD, you can enable443redirection as well::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 444–454why, one step back444445 >>> r = requests.post('http://github.com', allow_redirects=True)446 >>> r.url447 'https://github.com/'448 >>> r.history449 [<Response [301]>]450451452Timeouts453--------454Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 455–456why, one step back455You can tell requests to stop waiting for a response after a given number of456seconds with the ``timeout`` parameter::Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 457–462why, one step back457458 >>> requests.get('http://github.com', timeout=0.001)459 Traceback (most recent call last):460 File "<stdin>", line 1, in <module>461 requests.exceptions.Timeout: Request timed out.462Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
line 463why, one step back463.. admonition:: Note:Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
line 464why, one step back464Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 465–466why, one step back465 ``timeout`` only effects the connection process itself, not the466 downloading of the response body.Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 467–479why, one step back467468469Errors and Exceptions470---------------------471472In the event of a network problem (e.g. DNS failure, refused connection, etc),473Requests will raise a :class:`ConnectionError` exception.474475In the event of the rare invalid HTTP response, Requests will raise476an :class:`HTTPError` exception.477478If a request times out, a :class:`Timeout` exception is raised.479Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 480–481why, one step back480If a request exceeds the configured number of maximum redirections, a481:class:`TooManyRedirects` exception is raised.Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 482–485why, one step back482483All exceptions that Requests explicitly raises inherit from484:class:`requests.exceptions.RequestException`.485Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f
lines 486–490why, one step back486You can refer to :ref:`Configuration API Docs <configurations>` for immediate487raising of :class:`HTTPError` exceptions via the ``danger_mode`` option or488have Requests catch the majority of489:class:`requests.exceptions.RequestException` exceptions with the ``safe_mode``490option.Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82
lines 491–494why, one step back491492-----------------------493494Ready for more? Check out the :ref:`advanced <advanced>` section.