update python_requests.py

master
xianhu 2016-11-02 10:26:49 +08:00
parent 3ea12f7f85
commit 2bb6691535
1 changed files with 17 additions and 13 deletions

View File

@ -51,6 +51,7 @@ print(r.raw.read(10)) # "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03"
with open("test", "wb") as fd: with open("test", "wb") as fd:
for chunk in r.iter_content(chunk_size=256): for chunk in r.iter_content(chunk_size=256):
fd.write(chunk) fd.write(chunk)
# 注意: 设置的timeout对connect和read起作用. 但一旦和服务器建立连接, r.content或r.iter_content就处于一个read的状态, 不受timeout影响
# 定制请求头: 一个字典 # 定制请求头: 一个字典
headers = {"user-agent": "my-app/0.0.1"} headers = {"user-agent": "my-app/0.0.1"}
@ -114,26 +115,26 @@ cookies = dict(cookies_are="working")
r = requests.get("http://httpbin.org/cookies", cookies=cookies) r = requests.get("http://httpbin.org/cookies", cookies=cookies)
print(r.text) print(r.text)
# 会话对象: 会话对象让你能够跨请求保持某些参数, 它也会在同一个 Session 实例发出的所有请求之间保持cookie # 会话对象: 会话对象让你能够跨请求保持某些参数, 它也会在同一个Session实例发出的所有请求之间保持cookie
s = requests.Session() s = requests.Session()
s.get("http://httpbin.org/cookies/set/sessioncookie/123456789") s.get("http://httpbin.org/cookies/set/sessioncookie/123456789")
r = s.get("http://httpbin.org/cookies") s.get("http://httpbin.org/cookies")
print(r.text) # '{"cookies": {"sessioncookie": "123456789"}}' for cookie in s.cookies:
print(cookie)
# 会话也可用来为请求方法提供缺省数据, 这是通过为会话对象的属性提供数据来实现的 # 会话也可用来为请求方法提供缺省数据, 这是通过为会话对象的属性提供数据来实现的
s = requests.Session()
s.auth = ("user", "pass") s.auth = ("user", "pass")
s.headers.update({"x-test": "true"}) 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
# 下面的例子只会和第一个请求发送cookie, 而非第二个 s.get("http://httpbin.org/cookies", cookies={"from-my": "browser"}) # 带有cookie
s = requests.Session() s.get("http://httpbin.org/cookies") # 不带cookie
r = s.get("http://httpbin.org/cookies", cookies={"from-my": "browser"})
print(r.text) # '{"cookies": {"from-my": "browser"}}' # 如果你要手动为会话添加cookie, 就是用Cookie utility函数来操纵Session.cookies
r = s.get("http://httpbin.org/cookies") requests.utils.add_dict_to_cookiejar(s.cookies, {"cookie_key": "cookie_value"})
print(r.text) # '{"cookies": {}}' for cookie in s.cookies:
# 如果你要手动为会话添加 cookie, 就是用 Cookie utility 函数来操纵Session.cookies print(cookie)
# 会话还可以用作前后文管理器 # 会话还可以用作前后文管理器
with requests.Session() as s: with requests.Session() as s:
@ -214,3 +215,6 @@ proxies = {
"https": "socks5://user:pass@host:port" "https": "socks5://user:pass@host:port"
} }
requests.get("http://example.org", proxies=proxies) requests.get("http://example.org", proxies=proxies)
# 关闭InsecurePlatformWarning
requests.packages.urllib3.disable_warnings()