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
Provenance
- Commit
f4f863f- Tree
5a98836- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
Kenneth Reitz <me@kennethreitz.org>- Parent
f422ace
1 file changed+32 added−15 removed
modified:docs/user/advanced.rst+32 added-15 removed
22 unmodified lines23 24 s = requests.Session()25 26 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')28 29 print(r.text)30 # '{"cookies": {"sessioncookie": "123456789"}}'31 17 unmodified lines49 requests, even if using a session. This example will only send the cookies50 with the first request, but not the second::51 52 s = requests.Session()53+54 r = s.get('http://httpbin.org/cookies', cookies={'from-my': 'browser'})55 print(r.text)56 # '{"cookies": {"from-my": "browser"}}'57 71 unmodified lines129 130 from requests import Request, Session131 132 s = Session()132- req = Request('GET', url,133- data=data,134- headers=header135- )133+134+ req = Request('POST', url, data=data, headers=headers)135 prepped = req.prepare()136 137 # do something with prepped.body138+ prepped.body = 'No, I want exactly this as the body.'139+140 # do something with prepped.headers141+ del prepped.headers['Content-Type']142 143 resp = s.send(prepped,144 stream=stream,145 verify=verify,346 unmodified lines492 the resulting iterator object instead::493 494 lines = r.iter_lines()495 # Save the first line for later or just skip it496+497 first_line = next(lines)498+499 for line in lines:500 print(line)501 502 .. _proxies:6 unmodified lines509 510 import requests511 512 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 }516 513- requests.get("http://example.org", proxies=proxies)517+ requests.get('http://example.org', proxies=proxies)518 519 You can also configure proxies by setting the environment variables520 ``HTTP_PROXY`` and ``HTTPS_PROXY``.521 3 unmodified lines525 $ export HTTPS_PROXY="http://10.10.1.10:1080"526 527 $ python528 >>> import requests525- >>> requests.get("http://example.org")529+ >>> requests.get('http://example.org')530 531 To use HTTP Basic Auth with your proxy, use the `http://user:password@host/` syntax::532 533 proxies = {530- "http": "http://user:pass@10.10.1.10:3128/",534+ 'http': 'http://user:pass@10.10.1.10:3128/',535 }536 537 To give a proxy for a specific scheme and host, use the538 `scheme://hostname` form for the key. This will match for1 unmodified line540 541 ::542 543 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 }546 547 Note that proxy URLs must include the scheme.548 57 unmodified lines606 607 ::608 609 >>> commit_data = r.json()610+611 >>> print(commit_data.keys())612 [u'committer', u'author', u'url', u'tree', u'sha', u'parents', u'message']613+614 >>> 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'}616+617 >>> print(commit_data[u'message'])618 makin' history619 620 So far, so simple. Well, let's investigate the GitHub API a little bit. Now,31 unmodified lines652 653 >>> r = requests.get('https://api.github.com/repos/kennethreitz/requests/issues/482')654 >>> r.status_code655 200656+657 >>> issue = json.loads(r.text)658+659 >>> print(issue[u'title'])660 Feature any http verb in docs661+662 >>> print(issue[u'comments'])663 3664 665 Cool, we have three comments. Let's take a look at the last of them.2 unmodified lines668 669 >>> r = requests.get(r.url + u'/comments')670 >>> r.status_code671 200672+673 >>> comments = r.json()674+675 >>> print(comments[0].keys())676 [u'body', u'url', u'created_at', u'updated_at', u'user', u'id']677+678 >>> print(comments[2][u'body'])679 Probably in the "advanced" section680 681 Well, that seems like a silly place. Let's post a comment telling the poster11 unmodified lines693 ::694 695 >>> 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"697+698 >>> r = requests.post(url=url, data=body)699 >>> r.status_code700 404701 4 unmodified lines706 ::707 708 >>> from requests.auth import HTTPBasicAuth709 >>> auth = HTTPBasicAuth('fake@example.com', 'not_a_real_password')710+711 >>> r = requests.post(url=url, data=body, auth=auth)712 >>> r.status_code713 201714+715 >>> content = r.json()716 >>> print(content[u'body'])717 Sounds great! I'll get right on it.718 5 unmodified lines724 ::725 726 >>> print(content[u"id"])727 5804413728+729 >>> 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"731+732 >>> r = requests.patch(url=url, data=body, auth=auth)733 >>> r.status_code734 200735 112 unmodified lines848 class Ssl3HttpAdapter(HTTPAdapter):849 """"Transport adapter" that allows us to use SSLv3."""850 851 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)855 856 .. _`described here`: http://www.kennethreitz.org/essays/the-future-of-python-http857 .. _`urllib3`: https://github.com/shazow/urllib3858 58 unmodified lines