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.0.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.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mipu_requests-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 a6334d2094971ef13051bcd158a78242667e3429ca0e31be1b4ee3cb8d237d68
MD5 6efb32bc4ee87a61ddd8411df6499850
BLAKE2b-256 16ef4b2077300e5d729ae919cdbe0cba56958441ada53879f129bc893facefc9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mipu_requests-1.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb5a7588e41b7ba6aedde36c9912aa6747e02f082fa55692db94271d197a5410
MD5 7764a21a00493819257bf261015c4d8e
BLAKE2b-256 a697c2f40d4350bacd41298e4b69d22f662986563163204c2c9cda24f0785c62

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