---
title: "Python 转换 cURL 完全指南：开发者实用手册"
description: "学习如何借助 Requests 库将 cURL 转换为 Python 代码，涵盖 GET 与 POST 请求、身份验证、文件上传、错误处理及转换工具的使用方法。"
url: "https://marsproxies.cn/blog/curl-to-python/"
---

- [EN ](https://marsproxies.com/blog/curl-to-python/?locale-change "英语")
  [CN](https://marsproxies.cn/blog/curl-to-python/?locale-change "语言切换器")

[联系销售](https://marsproxies.cn/contact-sales/) [注册](https://dashboard.marsproxies.com/register/) [登录](https://dashboard.marsproxies.com/login/)

[联系销售](https://marsproxies.cn/contact-sales/) [注册](https://dashboard.marsproxies.com/register/) [登录](https://dashboard.marsproxies.com/login/)

[联系销售](https://marsproxies.cn/contact-sales/)

[登录](https://dashboard.marsproxies.com/login/) [注册](https://dashboard.marsproxies.com/register/)

- [EN ](https://marsproxies.com/blog/curl-to-python/?locale-change "英语")
  [CN](https://marsproxies.cn/blog/curl-to-python/?locale-change "语言切换器")

[返回博客](https://marsproxies.cn/blog/) 2026年8月27日

# Python 转换 cURL 完全指南：开发者实用手册

[操作指南](https://marsproxies.cn/blog/category/how-to/)

\-

目录

1. [什么是 cURL？](https://marsproxies.cn/blog/curl-to-python/#b-%E4%BB%80%E4%B9%88%E6%98%AF-curl)
2. [理解 cURL 命令](https://marsproxies.cn/blog/curl-to-python/#b-%E7%90%86%E8%A7%A3-curl-%E5%91%BD%E4%BB%A4)
3. [如何手动将 cURL 转换为 Python](https://marsproxies.cn/blog/curl-to-python/#b-%E5%A6%82%E4%BD%95%E6%89%8B%E5%8A%A8%E5%B0%86-curl-%E8%BD%AC%E6%8D%A2%E4%B8%BA-python)
4. [转换 GET 请求](https://marsproxies.cn/blog/curl-to-python/#b-%E8%BD%AC%E6%8D%A2-get-%E8%AF%B7%E6%B1%82)
5. [转换 POST 请求](https://marsproxies.cn/blog/curl-to-python/#b-%E8%BD%AC%E6%8D%A2-post-%E8%AF%B7%E6%B1%82)
6. [身份验证方法](https://marsproxies.cn/blog/curl-to-python/#b-%E8%BA%AB%E4%BB%BD%E9%AA%8C%E8%AF%81%E6%96%B9%E6%B3%95)
7. [处理文件上传](https://marsproxies.cn/blog/curl-to-python/#b-%E5%A4%84%E7%90%86%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0)
8. [SSL 证书与错误处理](https://marsproxies.cn/blog/curl-to-python/#b-ssl-%E8%AF%81%E4%B9%A6%E4%B8%8E%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86)
9. [实际使用场景](https://marsproxies.cn/blog/curl-to-python/#b-%E5%AE%9E%E9%99%85%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF)
10. [调用 API（真实案例）](https://marsproxies.cn/blog/curl-to-python/#b-%E8%B0%83%E7%94%A8-api%E7%9C%9F%E5%AE%9E%E6%A1%88%E4%BE%8B)
11. [实现 HTTP 请求自动化](https://marsproxies.cn/blog/curl-to-python/#b-%E5%AE%9E%E7%8E%B0-http-%E8%AF%B7%E6%B1%82%E8%87%AA%E5%8A%A8%E5%8C%96)
12. [自动化的 cURL 转 Python 工具](https://marsproxies.cn/blog/curl-to-python/#b-%E8%87%AA%E5%8A%A8%E5%8C%96%E7%9A%84-curl-%E8%BD%AC-python-%E5%B7%A5%E5%85%B7)
13. [在线转换工具](https://marsproxies.cn/blog/curl-to-python/#b-%E5%9C%A8%E7%BA%BF%E8%BD%AC%E6%8D%A2%E5%B7%A5%E5%85%B7)
14. [Python 库](https://marsproxies.cn/blog/curl-to-python/#b-python-%E5%BA%93)
15. [结语](https://marsproxies.cn/blog/curl-to-python/#b-%E7%BB%93%E8%AF%AD)

\-

在终端中测试 API、下载文件或发送 HTTP 请求时，cURL 是最快捷的方式之一。不过，当任务需要循环、可复用逻辑、定时调度或错误处理时，终端命令就显得不够方便了。

正因如此，开发者常常将 cURL 转换为 Python 代码，并改用 Requests 库。本指南将介绍如何转换 cURL 命令、翻译常见选项、处理身份验证与文件上传，以及使用自动转换工具。

## 什么是 cURL？

cURL 是一种用于通过 URL 传输数据的命令行工具，支持 HTTP、HTTPS、FTP 等多种协议，开发者通常用它来调试 API、传输文件以及快速测试端点。

cURL 命令简洁，适合一次性运行。而当同一个请求需要成为应用程序或自动化脚本的一部分时，Python 请求通常更便于维护。将命令迁移到 Python 后，您可以在请求周围添加条件判断、循环、日志记录、重试机制、解析逻辑等程序控制逻辑。

| 特性 | cURL | Python requests |
| --- | --- | --- |
| 语法 | 终端命令 | Python 代码 |
| 易用性 | 适合快速测试 | 更适合编写可复用脚本 |
| 请求头与数据 | 使用 -H、-d 等参数 | 字典与函数参数 |
| 自动化 | Shell 脚本与定时任务 | 完整的 Python 生态系统 |
| 错误处理 | 退出码与 Shell 逻辑 | 异常与响应方法 |

## 理解 cURL 命令

大多数 cURL 命令遵循以下简单结构：

```javascript
curl [options] URL
```

常见选项包括：

- **-X** 用于指定方法，如 GET、POST、PUT 或 DELETE

* **-H** 或 **--header** 用于添加请求头

- **-d** 或 **--data** 用于发送请求数据

* **-u** 用于提供用户名和密码

- **-i** 用于在输出中包含响应头

一个基本的 GET 请求如下所示：

```javascript
curl "https://api.example.com/users?page=2"
```

一个带有请求头和请求体的 POST 请求可能如下所示：

```typescript
curl - X POST "https://api.example.com/users" \
 
- H "Content-Type: application/json" \
 
-d '{"name":"Alex"}'
```

理解每个标志控制哪个部分，能让将 cURL 命令转换为 Python 代码变得容易得多。URL 标识端点，而各个选项则定义了方法、请求头、凭据和请求体。

在将命令翻译为 Python 之前，先在脑海中把这些部分区分开来。这个简单的习惯能让较长的终端示例不再让人望而生畏。

## 如何手动将 cURL 转换为 Python

手动转换很有价值，因为它能帮助您理解 Python cURL 命令各部分是如何组合在一起的。一个好的做法是每次只转换一个选项。当自动转换工具生成的结果不符合预期时，这种方法也很有帮助。

一旦您能自行将常见命令映射为 Python 代码，调试生成的代码片段就会快得多。

### 转换 GET 请求

要转换一个简单的 GET 请求，只需导入 Requests 并调用 requests.get()：

```python
import requests
 
 
params = { "page" : 2 }
 
headers = { "Accept" : "application/json" }
 
 
r = requests.get(
 
    "https://api.example.com/users" ,
 
    params=params,
 
    headers=headers,
 
    timeout= 10
 
)
 
 
print (r.status_code)
 
```

查询参数放入 params 中，cURL 的 -H 值则变为 headers 字典中的条目。这是将命令翻译为 Python 代码最简单的方式之一，也能让最终的 URL 保持清晰易读。

对于更复杂的 GET 请求，您可以持续向 params 中添加键值对，而无需手动拼接冗长的查询字符串。

### 转换 POST 请求

在转换发送表单字段的 cURL 命令时，使用 data 参数：

```javascript
r = requests. post ( "https://api.example.com/login" , data= { "username" : "alex" , "password" : "secret" } )
```

对于 json 数据，则改用 json=：

```javascript
payload = { "name" : "Alex" }
 
 
r = requests. post (
 
    "https://api.example.com/users" ,
 
    json=payload,
 
    timeout= 10
 
)
```

这比手动序列化请求体并自行设置 JSON 内容类型要简洁得多。在将 POST 命令迁移到 Python 时，需先确认原始请求体是表单编码还是 JSON 格式，再决定使用 data= 还是 json=。

### 身份验证方法

cURL 的 -u 标志通常对应 HTTP 基本身份验证：

```python
import requests
 
from requests.auth import HTTPBasicAuth
 
 
r = requests.get(
 
    "https://api.example.com/account" ,
 
    auth=HTTPBasicAuth( "user" , "password" )
 
)
```

对于 Bearer 令牌，需将 cURL 的身份验证请求头转换为普通的 Python 字典：

```javascript
headers = { "Authorization" : "Bearer YOUR_TOKEN" }
 
r = requests. get ( "https://api.example.com/data" , headers=headers)
```

这使得身份验证信息在多个 Python 请求之间更易于复用。

### 处理文件上传

对于 multipart 上传，Requests 接受 files 参数：

```typescript
with open ( "report.pdf" , "rb" ) as file:
 
   response = requests. post (
 
        "https://api.example.com/upload" ,
 
        files= { "file" : file}
 
    )
```

需要注意的是，cURL 的 --upload-file 与 multipart 表单上传不同。在 HTTP(S) 场景下，它通常使用 PUT 方法执行上传。更贴近的 Python 等效写法是：

```typescript
with open ( "report.pdf" , "rb" ) as file:
 
    response = requests. put (
 
        "https://api.example.com/report.pdf" ,
 
        data=file
 
    )
```

了解这一区别可以避免 Python cURL 命令在不知不觉中改变请求格式。

### SSL 证书与错误处理

cURL 的 -k 会禁用证书验证。Requests 中最接近的选项是 verify=False，但只有在您充分理解其中的安全风险时才应使用。

```python
import requests
 
 
try :
 
    response = requests.get(
 
        "https://api.example.com/data" ,
 
        timeout= 10 ,
 
        verify= True
 
    )
 
    response.raise_for_status( )
 
except requests.exceptions.RequestException as exc:
 
    print ( f"Request failed:  {exc} " )
```

超时与异常处理是将 cURL 转换为脚本的重要原因之一。Python requests 能让您清晰地掌控失败情况，而不必将错误处理完全交给 Shell 逻辑。

## 实际使用场景

### 调用 API（真实案例）

假设有一个带有 Bearer 令牌、自定义请求头和 JSON 请求体的 API 调用：

```typescript
curl - X POST "https://api.example.com/tasks" \
 
 - H "Authorization: Bearer YOUR_TOKEN" \
 
 - H "Content-Type: application/json" \
 
 -d '{"title":"Check prices","active":true}'
```

可以将 cURL 转换为如下代码：

```python
import requests
 
 
url = "https://api.example.com/tasks"
 
headers = { "Authorization" : "Bearer YOUR_TOKEN" }
 
payload = { "title" : "Check prices" , "active" : True }
 
 
response = requests.post(url, headers=headers, json=payload, timeout= 10 )
 
response.raise_for_status( )
 
 
print (response.json( ) )
```

Python 代码更长，但更易于扩展。您可以验证响应、保存结果、添加代理设置，或对多条记录重复发送该请求。

### 实现 HTTP 请求自动化

一次性的终端命令固然方便，但重复性工作正是命令转换为 Python 代码更有价值的地方。脚本可以从文件中读取输入、发送数百个 Python 请求、处理每个响应，并将结果写入数据库。

随后，您可以在 Linux 或 macOS 上通过 cron 运行该脚本，在 Windows 上使用任务计划程序，或借助编排平台运行。这也是开发者从测试阶段转向生产自动化时，常常需要转换 cURL 命令的原因之一。

## 自动化的 cURL 转 Python 工具

### 在线转换工具

当您需要快速转换 cURL 命令时，cURL Converter 是一个便捷的选择。将命令粘贴到输入框中，按需选择 Python，然后复制生成的 Python 代码即可。它同样支持粘贴从浏览器开发者工具中复制的命令。

一个简单的使用流程是：

1. 复制 cURL 命令
2. 粘贴到 cURL Converter 中
3. 检查生成的 Python cURL 命令
4. 核对请求头、Cookie、请求体和重定向情况
5. 在投入生产环境前先测试代码

转换工具能节省时间，但请务必检查输出结果。默认行为上的差异可能带来影响，尤其是在重定向、Cookie 和不常见的 cURL 选项方面。

### Python 库

uncurl 是一个专为将 cURL 请求转换为 Python Requests 语法而设计的 Python 包，可通过以下方式安装：

```javascript
pip install uncurl
```

然后在 Python 中使用：

```python
import uncurl
 
command = "curl https://example.com -H 'Accept: application/json'"
print (uncurl.parse(command) )
```

当您需要在 Python 工作流中转换 cURL 命令时，这个包会很有用。不过，该包自 2021 年以来未在 PyPI 上发布新版本，因此请仔细测试生成的代码，如需更广泛且持续维护的转换支持，可考虑使用 cURL Converter。

## 结语

学习将 cURL 转换为 Python，本质上就是将命令行选项翻译为 Requests 参数：请求头对应字典，查询字符串对应 params，请求体对应 data 或 json，身份验证则对应 headers 或 auth。

先从自行掌握转换逻辑开始，逐条命令地将 cURL 转换为 Python 代码。等您熟练掌握这种转换方式后，再借助自动化工具加快重复性工作，而不会让整个过程变成一个看不透的黑箱。

不妨亲自动手尝试一些 cURL 命令，比如 GET、POST、身份验证、文件上传和错误处理，最后再参考 cURL 与 requests 的官方文档。

[了解更多](https://dashboard.marsproxies.com/register/)

\-

目录

1. [什么是 cURL？](https://marsproxies.cn/blog/curl-to-python/#b-%E4%BB%80%E4%B9%88%E6%98%AF-curl)
2. [理解 cURL 命令](https://marsproxies.cn/blog/curl-to-python/#b-%E7%90%86%E8%A7%A3-curl-%E5%91%BD%E4%BB%A4)
3. [如何手动将 cURL 转换为 Python](https://marsproxies.cn/blog/curl-to-python/#b-%E5%A6%82%E4%BD%95%E6%89%8B%E5%8A%A8%E5%B0%86-curl-%E8%BD%AC%E6%8D%A2%E4%B8%BA-python)
4. [转换 GET 请求](https://marsproxies.cn/blog/curl-to-python/#b-%E8%BD%AC%E6%8D%A2-get-%E8%AF%B7%E6%B1%82)
5. [转换 POST 请求](https://marsproxies.cn/blog/curl-to-python/#b-%E8%BD%AC%E6%8D%A2-post-%E8%AF%B7%E6%B1%82)
6. [身份验证方法](https://marsproxies.cn/blog/curl-to-python/#b-%E8%BA%AB%E4%BB%BD%E9%AA%8C%E8%AF%81%E6%96%B9%E6%B3%95)
7. [处理文件上传](https://marsproxies.cn/blog/curl-to-python/#b-%E5%A4%84%E7%90%86%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0)
8. [SSL 证书与错误处理](https://marsproxies.cn/blog/curl-to-python/#b-ssl-%E8%AF%81%E4%B9%A6%E4%B8%8E%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86)
9. [实际使用场景](https://marsproxies.cn/blog/curl-to-python/#b-%E5%AE%9E%E9%99%85%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF)
10. [调用 API（真实案例）](https://marsproxies.cn/blog/curl-to-python/#b-%E8%B0%83%E7%94%A8-api%E7%9C%9F%E5%AE%9E%E6%A1%88%E4%BE%8B)
11. [实现 HTTP 请求自动化](https://marsproxies.cn/blog/curl-to-python/#b-%E5%AE%9E%E7%8E%B0-http-%E8%AF%B7%E6%B1%82%E8%87%AA%E5%8A%A8%E5%8C%96)
12. [自动化的 cURL 转 Python 工具](https://marsproxies.cn/blog/curl-to-python/#b-%E8%87%AA%E5%8A%A8%E5%8C%96%E7%9A%84-curl-%E8%BD%AC-python-%E5%B7%A5%E5%85%B7)
13. [在线转换工具](https://marsproxies.cn/blog/curl-to-python/#b-%E5%9C%A8%E7%BA%BF%E8%BD%AC%E6%8D%A2%E5%B7%A5%E5%85%B7)
14. [Python 库](https://marsproxies.cn/blog/curl-to-python/#b-python-%E5%BA%93)
15. [结语](https://marsproxies.cn/blog/curl-to-python/#b-%E7%BB%93%E8%AF%AD)

分类

- [产品动态](https://marsproxies.cn/blog/category/product-news/)

  [5](https://marsproxies.cn/blog/category/product-news/)
- [产品对比](https://marsproxies.cn/blog/category/comparisons/)

  [3](https://marsproxies.cn/blog/category/comparisons/)
- [反检测解决方案](https://marsproxies.cn/blog/category/antidetect-solution/)

  [3](https://marsproxies.cn/blog/category/antidetect-solution/)
- [操作指南](https://marsproxies.cn/blog/category/how-to/)

  [115](https://marsproxies.cn/blog/category/how-to/)
- [球鞋抢购机器人](https://marsproxies.cn/blog/category/sneaker-bots/)

  [2](https://marsproxies.cn/blog/category/sneaker-bots/)
- [资讯](https://marsproxies.cn/blog/category/insights/)

  [87](https://marsproxies.cn/blog/category/insights/)

## 相关文章

[操作指南](https://marsproxies.cn/blog/category/how-to/)

### [如何在 PS5 上设置代理服务器（分步指南）](https://marsproxies.cn/blog/proxy-server-on-ps5/)

学习如何在 PS5 上设置代理服务器，按照本指南配置代理设置、提升下载速度并绕过封锁。

[Lukas Tilkevicius 增长与合作经理](https://marsproxies.cn/blog/authors/lukas-tilkevicius/)

2026年10月7日

[操作指南](https://marsproxies.cn/blog/category/how-to/)

### [如何使用 Node Unblocker 进行数据抓取与隐私保护](https://marsproxies.cn/blog/node-unblocker/)

了解如何使用 Node Unblocker 进行数据抓取、保护隐私并绕过访问限制，本文将介绍其搭建步骤、使用场景、优势与主要局限性。

[Dalius Pamedytis 软件工程师](https://marsproxies.cn/blog/authors/dalius-pamedytis/)

2026年10月6日

[操作指南](https://marsproxies.cn/blog/category/how-to/)

### [如何修复 Python 中的 externally-managed-environment 错误](https://marsproxies.cn/blog/externally-managed-environment/)

修复 Python 中的 externally-managed-environment 错误，了解 pip 为何阻止系统 Python 安装，以及虚拟环境如何解决这一问题。

[Lukas Tilkevicius 增长与合作经理](https://marsproxies.cn/blog/authors/lukas-tilkevicius/)

2026年10月5日
