> For the complete documentation index, see [llms.txt](https://nining.website/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nining.website/python/spider/urllib3.md).

# urllib3源码泛读

> urllib3是一个功能强大，方便使用，Python实现的HTTP客户端

* 英文官方:  <https://urllib3.readthedocs.io/en/latest/index.html>
* 官方中文:  <https://s0urllib30readthedocs0io.icopy.site/en/latest/index.html>
* 源码链接:  <https://github.com/urllib3/urllib3>

### 基本使用

```python
import urllib3
import json

http = urllib3.PoolManager()
r = http.request('GET', 'http://httpbin.org/get', fields={'key': 'value'})
print(r.status)
print(json.loads(r.data.decode('utf-8')))
print(r.headers)
```

### 源码分析

分析\_\_init\_\_.py就可以得出对外提供的功能

```python
__all__ = (
    'HTTPConnectionPool',   # http模式连接池
    'HTTPSConnectionPool',  # https模式连接池
    'PoolManager',          # 池管理类，self.poos映射类型，保存连接信息
    'ProxyManager',         # 行为同PoolManager
    'HTTPResponse',         # 返回对象
    'Retry',                # 精细化控制重试和重定向 retries=Retry(3, redirect=2)
    'Timeout',              # 精细化控制超时 timeout=Timeout(connect=1.0, read=2.0)
    'add_stderr_logger',    # 修改默认日志的级别
    'connection_from_url',  # 返回HTTPConnectionPool或HTTPSConnectionPool实例
    'disable_warnings',     # 禁用warnings
    'encode_multipart_formdata',    # dict 转换成 form-data
    'get_host',             # Deprecated. Use :func:`parse_url` instead
    'make_headers',         # 生成request headers 快捷函数
    'proxy_from_url',       # 返回ProxyManager对象
)
```

* Retry，Timeout 整数封装成附加其他功效的简单功能类
* HTTPResponse 对返回数据的Model封装

#### PoolManager与RequestMethods

![](/files/-LmndUjKtpnBS5Tvdb0Z)

#### 主干类的层次结构

![](/files/-LnMFlaBHC4xtreOwlvc)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nining.website/python/spider/urllib3.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
