# Python

> 使用 Proxyman Automatic Setup 抓取 Python 脚本和服务的 HTTP/HTTPS 流量，兼容 requests、http.client、urllib3、httpx 和 aiohttp。

## 1. 全新自动方案（v4.7.0 或更高版本）✅ {#1-new-automatic-solution-v470-or-later-}

Proxyman v4.7.0 或更高版本可以一键抓取 Python 的 HTTP/HTTPS 流量。

* 一键方案：无需手动设置 HTTP Proxy，也无需手动信任自签名证书。
* 支持多种 Python 库：requests、http.client、urllib3、httpx 和 aiohttp

### 使用方法： {#how-to-use}

1. 打开 Proxyman -> Setup Menu -> Automatic Setup
2. 点击 “Open New Terminal”
3. 如有需要，请允许 AppleScript 的权限请求
4. 会打开一个新的 Terminal 窗口，在其中启动 Python 后端服务或运行脚本，Proxyman 会自动抓取所有流量
5. 完成 ✅

![Proxyman Automatic Setup 窗口，其中的 Open New Terminal 按钮可以抓取 aiohttp、httplib 等 Python 库的流量](https://proxyman.com/docs/assets/proxyman-automatic-setup-window.jpg)

_使用 Proxyman Automatic Setup 抓取 Python 流量_

请查看 Automatic Setup 页面：

- [Automatic Setup（自动设置）](https://proxyman.com/docs/zh-cn/automatic-setup/automatic-setup)

## 2. 旧方案（不推荐）❌ {#2-old-solution-not-recommended-}

### 1. 脚本方式 {#1-script-approach}

1. 使用 [GitHub issue #1220 中的这个脚本](https://github.com/ProxymanApp/Proxyman/issues/1220#issuecomment-1249090359)，自动为 Python 安装或移除证书。
2. 将脚本以 `script.py` 为文件名保存到桌面文件夹中 

* 添加证书： 

```bash
$ python3 script.py add
```

* 移除证书

```
$ python3 script.py remove
```

> **备注:** 对于 macOS 12.2 或更高版本，请确保使用 `python3`
>
> 致谢 [@novitae](https://github.com/novitae)

### 2. 手动方式 {#2-manual-approach}

#### 在 Python 环境中安装 Proxyman 证书 {#install-the-proxyman-certificate-in-the-python-environment}

默认情况下，macOS 上的 Python 不信任 Proxyman 自签名证书，因此在拦截 HTTPS 流量时可能会出现 SSL 错误。 

要拦截 Python 脚本的 HTTPS 流量，你需要明确**告诉 Python 使用位于 `~/.proxyman/proxyman-ca.pem` 的 Proxyman 根证书**。

> **备注:** 较新版本的 Proxyman 将证书存放在 `~/Library/Application Support/com.proxyman.NSProxy/app-data/proxyman-ca.pem`。如果你的 Mac 上不存在 `~/.proxyman/proxyman-ca.pem`，请在下面的命令中改用该路径，或者从 Certificate 菜单 -> Export -> Root Certificate as PEM 导出证书。

请按以下步骤操作：

1. 在 Mac 上安装 Proxyman 证书（如已完成，请跳过此步骤；如未完成，请参阅 [macOS 指南](https://proxyman.com/docs/zh-cn/debug-devices/macos)）。
2. 在 Terminal 应用中运行以下命令

```bash
$ export SSL_CERT_FILE=~/.proxyman/proxyman-ca.pem
$ export REQUESTS_CA_BUNDLE=~/.proxyman/proxyman-ca.pem
$ echo "export REQUESTS_CA_BUNDLE=~/.proxyman/proxyman-ca.pem" >> ~/.bash_profile ; source ~/.bash_profile
```

3\. 完成。

#### 撤销更改 {#revert-the-change}

如果不再使用 Proxyman，请注释掉以下内容以撤销更改：

```bash
# export REQUESTS_CA_BUNDLE=~/.proxyman/proxyman-ca.pem
```

（在 \~/.bash\_profile 中）

## 3. 故障排查 {#3-troubleshooting}

### 3.1 Proxyman 无法抓取我的 Python 代码发出的 HTTP 流量。 {#31-proxyman-could-not-capture-http-traffic-from-my-python-code}

**解决方法**：请使用 [Automatic Setup](https://proxyman.com/docs/zh-cn/automatic-setup/automatic-setup)。

### 参考资料 {#reference}

* [https://github.com/ProxymanApp/Proxyman/issues/948#issuecomment-890520435](https://github.com/ProxymanApp/Proxyman/issues/948#issuecomment-890520435)
* [https://github.com/ProxymanApp/Proxyman/issues/1220](https://github.com/ProxymanApp/Proxyman/issues/1220)

## 了解更多 {#learn-more}

* [如何抓取 Python requests 或 httpx 库的 HTTPS 流量](https://proxyman.com/posts/2025-01-29-How-To-Capture-HTTPS-Traffic-From-Python-Requests-httpx)
* [使用 Proxyman 抓取 Python 的 HTTP/HTTPS 流量](https://proxyman.com/python)（Proxyman 官网）
