From a9e84f748e29fb21b815e8f755beb63a2e6c9885 Mon Sep 17 00:00:00 2001 From: xianhu Date: Tue, 1 Nov 2016 18:19:18 +0800 Subject: [PATCH 1/2] update python_requests.py --- python_requests.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/python_requests.py b/python_requests.py index c6b905c..68b482d 100644 --- a/python_requests.py +++ b/python_requests.py @@ -114,26 +114,26 @@ r = requests.get("http://httpbin.org/cookies", cookies=cookies) print(r.text) -# 会话对象: 会话对象让你能够跨请求保持某些参数, 它也会在同一个 Session 实例发出的所有请求之间保持cookie +# 会话对象: 会话对象让你能够跨请求保持某些参数, 它也会在同一个Session实例发出的所有请求之间保持cookie s = requests.Session() s.get("http://httpbin.org/cookies/set/sessioncookie/123456789") -r = s.get("http://httpbin.org/cookies") -print(r.text) # '{"cookies": {"sessioncookie": "123456789"}}' +s.get("http://httpbin.org/cookies") +for cookie in s.cookies: + print(cookie) # 会话也可用来为请求方法提供缺省数据, 这是通过为会话对象的属性提供数据来实现的 -s = requests.Session() s.auth = ("user", "pass") s.headers.update({"x-test": "true"}) -s.get("http://httpbin.org/headers", headers={"x-test2": "true"}) # both "x-test" and "x-test2" are sent +s.get("http://httpbin.org/headers", headers={"x-test2": "true"}) # both "x-test" and "x-test2" are sent -# 不过需要注意, 就算使用了会话, 方法级别的参数也不会被跨请求保持 -# 下面的例子只会和第一个请求发送cookie, 而非第二个 -s = requests.Session() -r = s.get("http://httpbin.org/cookies", cookies={"from-my": "browser"}) -print(r.text) # '{"cookies": {"from-my": "browser"}}' -r = s.get("http://httpbin.org/cookies") -print(r.text) # '{"cookies": {}}' -# 如果你要手动为会话添加 cookie, 就是用 Cookie utility 函数来操纵Session.cookies +# 不过需要注意, 就算使用了会话, 方法级别的参数也不会被跨请求保持, 下面的例子只会给第一个请求发送cookie +s.get("http://httpbin.org/cookies", cookies={"from-my": "browser"}) # 带有cookie +s.get("http://httpbin.org/cookies") # 不带cookie + +# 如果你要手动为会话添加cookie, 就是用Cookie utility函数来操纵Session.cookies +requests.utils.add_dict_to_cookiejar(s.cookies, {"cookie_key": "cookie_value"}) +for cookie in s.cookies: + print(cookie) # 会话还可以用作前后文管理器 with requests.Session() as s: From 68479870f2772fc26095673cd4080804c9308160 Mon Sep 17 00:00:00 2001 From: cuican-wang Date: Sat, 5 Nov 2016 14:47:19 -0400 Subject: [PATCH 2/2] Update python_base.py --- python_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_base.py b/python_base.py index 4fd2792..c1d498f 100644 --- a/python_base.py +++ b/python_base.py @@ -72,7 +72,7 @@ #-- 集合set """ set是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素。 - set支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算。 + set支持union(联合), intersection(交), difference(差)和symmetric difference(对称差集)等数学运算。 set支持x in set, len(set), for x in set。 set不记录元素位置或者插入点, 因此不支持indexing, slicing, 或其它类序列的操作 """