Skip to content
Myr

Merge pull request #2712 from lukasgraf/master

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

Document use of sessions as context managers

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 9b067db, 8f3ddbe · Browse files · 2 files+21 added−1 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+21 added−1 removed

  • other:+21 added−1 removed

2 files+21 added−1 removed

modified:docs/user/advanced.rst+15 added-1 removed
10 unmodified lines
11---------------1213The Session object allows you to persist certain parameters across14requests. It also persists cookies across all requests made from the15Session instance.15Session instance, and will use ``urllib3``'s `connection pooling`_. So if16you're making several requests to the same host, the underlying TCP17connection will be reused, which can result in a significant performance18increase (see `HTTP persistent connection`_).1920A Session object has all the methods of the main Requests API.2122Let's persist some cookies across requests::
21 unmodified lines
44Any dictionaries that you pass to a request method will be merged with the45session-level values that are set. The method-level parameters override session46parameters.4748Sessions can also be used as context managers::4950 with requests.Session() as s:51 s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')5253This will make sure the session is closed as soon as the ``with`` block is54exited, even if unhandled exceptions occured.555657.. admonition:: Remove a Value From a Dict Parameter5859 Sometimes you'll want to omit session-level keys from a dict parameter. To60 do this, you simply set that key's value to ``None`` in the method-level
777 unmodified lines
838system.839840For the sake of security we recommend upgrading certifi frequently!841842.. _HTTP persistent connection: https://en.wikipedia.org/wiki/HTTP_persistent_connection843.. _connection pooling: https://urllib3.readthedocs.org/en/latest/pools.html844.. _certifi: http://certifi.io/845.. _Mozilla trust store: https://hg.mozilla.org/mozilla-central/raw-file/tip/security/nss/lib/ckfw/builtins/certdata.txt
modified:requests/sessions.py+6 added
273 unmodified lines
274 >>> import requests275 >>> s = requests.Session()276 >>> s.get('http://httpbin.org/get')277 200278279 Or as a context manager::280281 >>> with requests.Session() as s:282 >>> s.get('http://httpbin.org/get')283 200284 """285286 __attrs__ = [287 'headers', 'cookies', 'auth', 'proxies', 'hooks', 'params', 'verify',
396 unmodified lines