update docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent 0665129 · Browse files · 2 files·+19 added−27 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+19 added−27 removed
- other:+19 added−27 removed
Provenance
- Commit
5b5ff69- Tree
37b5b38- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
0665129
2 files changed+19 added−27 removed
modified:docs/user/advanced.rst+10 added-18 removed
15 unmodified lines16 A session object has all the methods of the main Requests API.17 18 Let's persist some cookies across requests::19 20- s = requests.session()20+ s = requests.Session()21 22 s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')23 r = s.get("http://httpbin.org/cookies")24 3 unmodified lines28 29 Sessions can also be used to provide default data to the request methods. This30 is done by providing data to the properties on a session object::31 32- headers = {'x-test': 'true'}33- auth = ('user', 'pass')32+ s = requests.Session()33+ s.auth = ('user', 'pass')34+ s.headers.update({'x-test': 'true'})35 35- with requests.session() as c:36- c.auth = auth37- c.headers = headers36+ # both 'x-test' and 'x-test2' are sent37+ s.get('http://httpbin.org/headers', headers={'x-test2': 'true'})38 39- # both 'x-test' and 'x-test2' are sent40- c.get('http://httpbin.org/headers', headers={'x-test2': 'true'})39 42-40 Any dictionaries that you pass to a request method will be merged with the session-level values that are set. The method-level parameters override session parameters.41 42 .. admonition:: Remove a Value From a Dict Parameter43 11 unmodified lines55 contains all of the information returned by the server and also contains the56 ``Request`` object you created originally. Here is a simple request to get some57 very important information from Wikipedia's servers::58 62- >>> response = requests.get('http://en.wikipedia.org/wiki/Monty_Python')59+ >>> r = requests.get('http://en.wikipedia.org/wiki/Monty_Python')60 61 If we want to access the headers the server sent back to us, we do this::62 66- >>> response.headers63+ >>> r.headers64 {'content-length': '56170', 'x-content-type-options': 'nosniff', 'x-cache':65 'HIT from cp1006.eqiad.wmnet, MISS from cp1010.eqiad.wmnet', 'content-encoding':66 'gzip', 'age': '3080', 'content-language': 'en', 'vary': 'Accept-Encoding,Cookie',67 'server': 'Apache', 'last-modified': 'Wed, 13 Jun 2012 01:33:50 GMT',4 unmodified lines72 73 However, if we want to get the headers we sent the server, we simply access the74 request, and then the request's headers::75 79- >>> response.request.headers76+ >>> r.request.headers77 {'Accept-Encoding': 'identity, deflate, compress, gzip',78 'Accept': '*/*', 'User-Agent': 'python-requests/0.13.1'}79 80 47 unmodified lines128 ...129 130 You can further control the workflow by use of the :class:`Response.iter_content` and :class:`Response.iter_lines` methods, or reading from the underlying urllib3 :class:`urllib3.HTTPResponse` at :class:`Response.raw`.131 135-Configuring Requests136---------------------132 138-Sometimes you may want to configure a request to customize its behavior. To do139-this, you can pass in a ``config`` dictionary to a request or session. See the :ref:`Configuration API Docs <configurations>` to learn more.140-141-133 Keep-Alive134 ----------135 136 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!57 unmodified lines194 195 ::196 197 from requests.auth import AuthBase198+199 class PizzaAuth(AuthBase):200 """Attaches HTTP Pizza Authentication to the given Request object."""201 def __init__(self, username):202 # setup any auth-related data here286 unmodified linesmodified:docs/user/quickstart.rst+9 added-9 removed
17 unmodified lines18 Let's get started with some simple examples.19 20 21 Make a Request22-------------------22+--------------23 24 Making a request with Requests is very simple.25 26 Begin by importing the Requests module::147 unmodified lines174 >>> payload = {'key1': 'value1', 'key2': 'value2'}175 >>> r = requests.post("http://httpbin.org/post", data=payload)176 >>> print r.text177 {178- // ...snip... //178+ ...179 "form": {180 "key2": "value2",181 "key1": "value1"182 },183- // ...snip... //183+ ...184 }185 186 There 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.187 16 unmodified lines204 205 >>> r = requests.post(url, files=files)206 >>> r.text207 {208- // ...snip... //208+ ...209 "files": {210 "file": "<censored...binary...data>"211 },212- // ...snip... //212+ ...213 }214 215 You can set the filename explicitly::216 2 unmodified lines219 220 >>> r = requests.post(url, files=files)221 >>> r.text222 {223- // ...snip... //223+ ...224 "files": {225 "file": "<censored...binary...data>"226 },227- // ...snip... //227+ ...228 }229 230 If you want, you can send strings to be received as files::231 2 unmodified lines234 235 >>> r = requests.post(url, files=files)236 >>> r.text237 {238- // ...snip... //238+ ...239 "files": {240 "file": "some,data,to,send\\nanother,row,to,send\\n"241 },242- // ...snip... //242+ ...243 }244 245 246 Response Status Codes175 unmodified lines