Skip to content
Myr

cleanup advanced.rst

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

single-quoted strings, yo!

authored by Kenneth Reitz · committed by Kenneth Reitz · parent f422ace · Browse files · 1 file+32 added−15 removed
This commit

State

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

Diff stats

1 file+32 added−15 removed

  • other:+32 added−15 removed

1 file+32 added−15 removed

modified:docs/user/advanced.rst+32 added-15 removed
22 unmodified lines
2324 s = requests.Session()2526 s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')27 r = s.get("http://httpbin.org/cookies")27 r = s.get('http://httpbin.org/cookies')2829 print(r.text)30 # '{"cookies": {"sessioncookie": "123456789"}}'31
17 unmodified lines
49requests, even if using a session. This example will only send the cookies50with the first request, but not the second::5152 s = requests.Session()5354 r = s.get('http://httpbin.org/cookies', cookies={'from-my': 'browser'})55 print(r.text)56 # '{"cookies": {"from-my": "browser"}}'57
71 unmodified lines
129130 from requests import Request, Session131132 s = Session()132 req = Request('GET', url,133 data=data,134 headers=header135 )133134 req = Request('POST', url, data=data, headers=headers)135 prepped = req.prepare()136137 # do something with prepped.body138 prepped.body = 'No, I want exactly this as the body.'139140 # do something with prepped.headers141 del prepped.headers['Content-Type']142143 resp = s.send(prepped,144 stream=stream,145 verify=verify,
346 unmodified lines
492 the resulting iterator object instead::493494 lines = r.iter_lines()495 # Save the first line for later or just skip it496497 first_line = next(lines)498499 for line in lines:500 print(line)501502.. _proxies:
6 unmodified lines
509510 import requests511512 proxies = {509 "http": "http://10.10.1.10:3128",510 "https": "http://10.10.1.10:1080",513 'http': 'http://10.10.1.10:3128',514 'https': 'http://10.10.1.10:1080',515 }516513 requests.get("http://example.org", proxies=proxies)517 requests.get('http://example.org', proxies=proxies)518519You can also configure proxies by setting the environment variables520``HTTP_PROXY`` and ``HTTPS_PROXY``.521
3 unmodified lines
525 $ export HTTPS_PROXY="http://10.10.1.10:1080"526527 $ python528 >>> import requests525 >>> requests.get("http://example.org")529 >>> requests.get('http://example.org')530531To use HTTP Basic Auth with your proxy, use the `http://user:password@host/` syntax::532533 proxies = {530 "http": "http://user:pass@10.10.1.10:3128/",534 'http': 'http://user:pass@10.10.1.10:3128/',535 }536537To give a proxy for a specific scheme and host, use the538`scheme://hostname` form for the key. This will match for
1 unmodified line
540541::542543 proxies = {540 "http://10.20.1.128": "http://10.10.1.10:5323",544 'http://10.20.1.128': 'http://10.10.1.10:5323',545 }546547Note that proxy URLs must include the scheme.548
57 unmodified lines
606607::608609 >>> commit_data = r.json()610611 >>> print(commit_data.keys())612 [u'committer', u'author', u'url', u'tree', u'sha', u'parents', u'message']613614 >>> print(commit_data[u'committer'])615 {u'date': u'2012-05-10T11:10:50-07:00', u'email': u'me@kennethreitz.com', u'name': u'Kenneth Reitz'}616617 >>> print(commit_data[u'message'])618 makin' history619620So far, so simple. Well, let's investigate the GitHub API a little bit. Now,
31 unmodified lines
652653 >>> r = requests.get('https://api.github.com/repos/kennethreitz/requests/issues/482')654 >>> r.status_code655 200656657 >>> issue = json.loads(r.text)658659 >>> print(issue[u'title'])660 Feature any http verb in docs661662 >>> print(issue[u'comments'])663 3664665Cool, we have three comments. Let's take a look at the last of them.
2 unmodified lines
668669 >>> r = requests.get(r.url + u'/comments')670 >>> r.status_code671 200672673 >>> comments = r.json()674675 >>> print(comments[0].keys())676 [u'body', u'url', u'created_at', u'updated_at', u'user', u'id']677678 >>> print(comments[2][u'body'])679 Probably in the "advanced" section680681Well, that seems like a silly place. Let's post a comment telling the poster
11 unmodified lines
693::694695 >>> body = json.dumps({u"body": u"Sounds great! I'll get right on it!"})696 >>> url = u"https://api.github.com/repos/kennethreitz/requests/issues/482/comments"697698 >>> r = requests.post(url=url, data=body)699 >>> r.status_code700 404701
4 unmodified lines
706::707708 >>> from requests.auth import HTTPBasicAuth709 >>> auth = HTTPBasicAuth('fake@example.com', 'not_a_real_password')710711 >>> r = requests.post(url=url, data=body, auth=auth)712 >>> r.status_code713 201714715 >>> content = r.json()716 >>> print(content[u'body'])717 Sounds great! I'll get right on it.718
5 unmodified lines
724::725726 >>> print(content[u"id"])727 5804413728729 >>> body = json.dumps({u"body": u"Sounds great! I'll get right on it once I feed my cat."})730 >>> url = u"https://api.github.com/repos/kennethreitz/requests/issues/comments/5804413"731732 >>> r = requests.patch(url=url, data=body, auth=auth)733 >>> r.status_code734 200735
112 unmodified lines
848 class Ssl3HttpAdapter(HTTPAdapter):849 """"Transport adapter" that allows us to use SSLv3."""850851 def init_poolmanager(self, connections, maxsize, block=False):834 self.poolmanager = PoolManager(num_pools=connections,835 maxsize=maxsize,836 block=block,837 ssl_version=ssl.PROTOCOL_SSLv3)852 self.poolmanager = PoolManager(853 num_pools=connections, maxsize=maxsize,854 block=block, ssl_version=ssl.PROTOCOL_SSLv3)855856.. _`described here`: http://www.kennethreitz.org/essays/the-future-of-python-http857.. _`urllib3`: https://github.com/shazow/urllib3858
58 unmodified lines