Skip to main content

米普代理 HTTP 客户端,兼容 requests 接口

Project description

mipu-requests

米普代理 HTTP 客户端,兼容 requests 接口。

所有 HTTP 请求通过米普 API 转发,接口与 requests 完全一致,迁移只需改 importproxies

安装

pip install mipu-requests

快速开始

import mipu_requests

s = mipu_requests.session(
    base_url="https://api-eu.mipuyun.com",
    client_key="your_client_key",
    client_secret="your_client_secret",
    agent="JQ",
)
s.proxies = "res_mipu_bookxWdyg"

resp = s.get("https://example.com/page")
resp.raise_for_status()
data = resp.json()

从 requests 迁移

# ── 迁移前 ──────────────────────────
import requests

s = requests.Session()
s.proxies = {"https": "http://proxy:8080"}
resp = s.get("https://example.com/api")

# ── 迁移后 ──────────────────────────
import mipu_requests

s = mipu_requests.session(
    base_url="https://api-eu.mipuyun.com",
    client_key="your_key",
    client_secret="your_secret",
    agent="JQ",
)
s.proxies = "res_mipu_bookxWdyg"
resp = s.get("https://example.com/api")

只需两步:

  1. import requestsimport mipu_requests
  2. 构造时传入凭据,proxies 改为字符串

其余代码(.get(), .post(), .json(), .raise_for_status() 等)无需改动。

请求方法

resp = s.get(url, params={"key": "value"}, headers={...})
resp = s.post(url, data={"key": "value"}, headers={...})
resp = s.post(url, json={"key": "value"}, headers={...})
resp = s.put(url, data=body, headers={...})
resp = s.delete(url, headers={...})

异步模式

默认启用异步轮询,防止长连接断连:

# 默认异步
s = mipu_requests.session(..., async_mode=True)

# 关闭异步
s = mipu_requests.session(..., async_mode=False)

# 自定义轮询参数
s = mipu_requests.session(
    ...,
    poll_interval=3,    # 轮询间隔(秒)
    poll_max=20,        # 最大轮询次数
    submit_timeout=60,  # 提交超时(秒)
    poll_timeout=(5, 30),  # 轮询超时 (connect, read)
)

Response 对象

requests.Response 完全兼容:

resp.status_code     # int   HTTP 状态码
resp.text            # str   响应体文本
resp.content         # bytes 响应体字节
resp.headers         # dict  响应头
resp.ok              # bool  status_code 在 200-399
resp.url             # str   请求 URL
resp.json()          # dict  解析 JSON
resp.raise_for_status()  # >=400 抛异常

异常处理

from mipu_requests import RequestException, HTTPError, APIError, PollTimeoutError

try:
    resp = s.get(url)
    resp.raise_for_status()
except HTTPError as e:
    print(f"目标站错误: {e.response.status_code}")
except APIError as e:
    print(f"API 错误: {e}")
except PollTimeoutError as e:
    print(f"轮询超时: {e}")
except RequestException as e:
    print(f"请求异常: {e}")

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mipu_requests-1.0.1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mipu_requests-1.0.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file mipu_requests-1.0.1.tar.gz.

File metadata

  • Download URL: mipu_requests-1.0.1.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for mipu_requests-1.0.1.tar.gz
Algorithm Hash digest
SHA256 441061dd7aa90f83120d4dd62a6e182fd2fa60b3b1b8ffa5a35a3d080f608ba5
MD5 dd58517db8e833b23a0527519336d18d
BLAKE2b-256 9b712c80e7165a3ee69b46db0d7b450f81a1900f4879c2029e7a6e56fa91e8ed

See more details on using hashes here.

File details

Details for the file mipu_requests-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: mipu_requests-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for mipu_requests-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 721d1a58aa4e7181a87d457207d4b98d4d719378f3cdd6ded75344b43bd6c3be
MD5 f9de8742528502a1c77857f7fbf788f3
BLAKE2b-256 6e8d2d890be06c98f643c790b3127ddd79b061f1cbe98aa361516a5a96162ede

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page