Scripts2026年7月16日·1 分钟阅读

Requests — Python HTTP for Humans

Elegant HTTP library for Python with automatic content decoding, connection pooling, and session persistence.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 64/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Requests Overview
先审查命令
npx -y tokrepo@latest install 33752715-8135-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

Introduction

Requests is the most downloaded Python package for making HTTP calls. It provides an intuitive API that abstracts away the complexity of urllib3, connection pooling, and content negotiation so developers can focus on interacting with web services rather than managing sockets.

What Requests Does

  • Sends GET, POST, PUT, PATCH, DELETE, and HEAD requests with a single function call
  • Handles JSON encoding and decoding automatically via r.json()
  • Manages sessions with cookies, headers, and auth persisted across requests
  • Supports file uploads, streaming downloads, and multipart form data
  • Provides built-in authentication helpers for Basic, Digest, and token-based auth

Architecture Overview

Requests is a thin ergonomic layer on top of urllib3. Each call creates a PreparedRequest, passes it through a session (which applies default headers, cookies, and auth), sends it via an HTTPAdapter backed by a urllib3 PoolManager, and returns a Response object with decoded content. Connection pooling and keep-alive are handled transparently.

Setup & Configuration

  • Install from PyPI: pip install requests
  • Use requests.Session() to persist settings across multiple calls
  • Configure retries via HTTPAdapter(max_retries=Retry(total=3))
  • Set timeouts globally with session.request(..., timeout=(3.05, 27))
  • Use environment variables HTTP_PROXY / HTTPS_PROXY for proxy configuration

Key Features

  • Automatic content decoding with charset detection
  • Connection pooling and keep-alive by default
  • International domain name and URL support
  • Streaming request and response bodies for large payloads
  • Hook system for request and response inspection

Comparison with Similar Tools

  • httpx — async-first with HTTP/2, Requests stays sync-only but simpler
  • urllib3 — lower-level with more control, Requests wraps it for convenience
  • aiohttp — async client and server, Requests is sync and client-only
  • curl / pycurl — C-based with broader protocol support, Requests is pure Python

FAQ

Q: Does Requests support async? A: No. For async HTTP use httpx or aiohttp. Requests is designed for synchronous workflows.

Q: How do I handle rate limiting? A: Attach a retry strategy via HTTPAdapter with Retry(status_forcelist=[429], backoff_factor=1).

Q: Is Requests thread-safe? A: Session objects are not thread-safe. Use one Session per thread or protect access with a lock.

Q: How do I verify SSL certificates? A: Requests verifies SSL by default using certifi. Pass verify=False to disable or verify='/path/to/ca-bundle' for custom CAs.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产