Skip to content

Commit 84c3342

Browse files
committed
update python_requests.py
1 parent 4ec4d1f commit 84c3342

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

python_requests.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
python_requests.py by xianhu
55
"""
66

7-
import requests
7+
import requests.adapters
88

99
# 不同方式获取网页内容, 返回一个Response对象, 请求的参数可以为url或Request对象
1010
r0 = requests.get("https://github.com/timeline.json")
@@ -181,7 +181,7 @@
181181
# 若请求超过了设定的最大重定向次数, 则会抛出一个 TooManyRedirects 异常
182182
# 所有Requests显式抛出的异常都继承自 requests.exceptions.RequestException
183183

184-
# 所有异常
184+
# 所有异常:
185185
# exception requests.RequestException(*args, **kwargs): There was an ambiguous exception that occurred while handling your request.
186186
# exception requests.ConnectionError(*args, **kwargs): A Connection error occurred.
187187
# exception requests.HTTPError(*args, **kwargs): An HTTP error occurred.
@@ -223,13 +223,25 @@ def print_url(resp):
223223
requests.get("http://example.org", proxies=proxies)
224224
# 若代理需要使用HTTP Basic Auth, 可以使用http://user:password@host:port/, 比如"http": "http://user:[email protected]:3128/"
225225

226-
# 除了基本的 HTTP 代理, Request 还支持 SOCKS 协议的代理
226+
# 除了基本的 HTTP 代理, Request 还支持 SOCKS 协议的代理, 此时需要单独安装:
227227
# $ pip install requests[socks]
228228
proxies = {
229229
"http": "socks5://user:pass@host:port",
230230
"https": "socks5://user:pass@host:port"
231231
}
232232
requests.get("http://example.org", proxies=proxies)
233233

234+
# Requests 传输适配器
235+
# 从 v1.0.0 以后,Requests 的内部采用了模块化设计。部分原因是为了实现传输适配器(Transport Adapter)。
236+
# 传输适配器提供了一个机制,让你可以为 HTTP 服务定义交互方法。尤其是它允许你应用服务前的配置。
237+
# Requests 自带了一个传输适配器,也就是 HTTPAdapter。 这个适配器使用了强大的 urllib3,为 Requests 提供了默认的 HTTP 和 HTTPS 交互。
238+
# 每当 Session 被初始化,就会有适配器附着在 Session 上,其中一个供 HTTP 使用,另一个供 HTTPS 使用。
239+
# Request 允许用户创建和使用他们自己的传输适配器,实现他们需要的特殊功能。创建好以后,传输适配器可以被加载到一个会话对象上,附带着一个说明,告诉会话适配器应该应用在哪个 web 服务上。
240+
s = requests.Session()
241+
s.mount("http://baidu.com", requests.adapters.HTTPAdapter())
242+
243+
# 出现错误: Connection pool is full, discarding connection: xxxx.com
244+
s.mount('https://', requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100))
245+
234246
# 关闭InsecurePlatformWarning
235247
# requests.packages.urllib3.disable_warnings()

0 commit comments

Comments
 (0)