跳到内容
hiproxy
开始使用

用 HiProxy 为 Python requests 设置代理

可直接复制的 Python requests、Node.js 和 cURL 示例。所有示例都使用 HiProxy 网关,您只需要修改用户名。

更新于 2026-11-28 · 阅读约 2 分钟

简要回答

在 Python requests 中传入 proxies 字典,http 和 https 使用同一个代理 URL,由用户名、密码、gate.hiproxy.shop 和 6100 端口组成。使用 SOCKS5 时,安装 requests[socks],并用 socks5h 和 6200 端口。不加 -sid- 时每个请求换新 IP,加上 -sid- 和 -ttl- 可保持同一 IP 最长 60 分钟。

Python requests 代理:基础示例

为 Python requests 设置代理只需要一个字典。在 http 和 https 两个键下放同一个代理 URL。https 键对应 https:// 地址,这些请求依然以隧道方式经过 HTTP 端口。

输出的地址就是请求使用的 IP。把 hpexample1 和 your_password 换成控制台中的代理用户名和密码。请始终设置超时,避免一个慢请求卡住整个脚本。

import requests

proxy = "http://hpexample1-zone-res-cc-us:your_password@gate.hiproxy.shop:6100"
proxies = {"http": proxy, "https": proxy}

r = requests.get("https://api.ipify.org", proxies=proxies, timeout=30)
print(r.text)

在 Python 中使用 SOCKS5

requests 支持 SOCKS 需要额外安装一个包:pip install requests[socks]。之后把协议改为 socks5h,端口改为 6200,其他都不变。

socks5h 表示由代理解析域名。如果用普通的 socks5,您的电脑会先做 DNS 查询,这个查询会从您自己的网络发出。

# pip install requests[socks]
import requests

proxy = "socks5h://hpexample1-zone-res-cc-us:your_password@gate.hiproxy.shop:6200"
proxies = {"http": proxy, "https": proxy}

r = requests.get("https://api.ipify.org", proxies=proxies, timeout=30)
print(r.text)

轮换和粘性会话

默认每个请求都获得新 IP,适合请求之间互不依赖的数据采集。要在多个请求中保持同一 IP,例如登录和之后的页面,加上带任意 ID 的 -sid-,再用 -ttl- 设置分钟数,最长 60。使用相同 ID 的请求都使用同一个 IP。

参数示例作用
-zone--zone-res产品:res、unl、isp 或 dc
-cc--cc-us出口国家,两位小写字母
-city--city-new_york城市,空格用下划线代替
-sid--sid-a1b2c3d4使用此 ID 的请求都用同一个 IP
-ttl--ttl-30会话保持 IP 的分钟数,1~60
import uuid
import requests

sid = uuid.uuid4().hex[:8]
user = f"hpexample1-zone-res-cc-us-sid-{sid}-ttl-30"
proxy = f"http://{user}:your_password@gate.hiproxy.shop:6100"

s = requests.Session()
s.proxies = {"http": proxy, "https": proxy}
print(s.get("https://api.ipify.org", timeout=30).text)
print(s.get("https://api.ipify.org", timeout=30).text)  # 同一个 IP

Node.js 和 cURL 示例

在 Node.js 中,undici 包通过 ProxyAgent 为 fetch 设置代理。使用 SOCKS5 时,socks-proxy-agent 包用 6200 端口的 socks5h:// URL 完成同样的工作。

使用 cURL 时,用 -x 指定代理,用 -U 指定登录信息:curl -x http://gate.hiproxy.shop:6100 -U "USERNAME:PASSWORD" https://api.ipify.org。使用 SOCKS5 时改用 socks5h://gate.hiproxy.shop:6200。不加 -sid- 运行两次,应该会看到两个不同的 IP。

import { ProxyAgent, fetch } from "undici";

const agent = new ProxyAgent(
  "http://hpexample1-zone-res-cc-us:your_password@gate.hiproxy.shop:6100"
);
const res = await fetch("https://api.ipify.org", { dispatcher: agent });
console.log(await res.text());

解决 407 错误和超时

记录失败请求的状态码和出口 IP。这样更容易分辨是代理登录问题,还是目标网站的拦截。

错误可能原因解决方法
407 Proxy Authentication Required(代理认证失败)代理用户名或密码错误,或参数有拼写错误从控制台重新复制两者。如果生成了新的代理密码,请更新脚本。
Connection timed out(连接超时)端口与协议不匹配,或防火墙拦截了出站连接http:// 用 6100,socks5h:// 用 6200。允许到 gate.hiproxy.shop 的出站流量。
Missing dependencies for SOCKS support(缺少 SOCKS 依赖)没有安装 requests[socks]运行 pip install requests[socks]。
IP 国家不对-cc- 不是两位小写字母代码代码用小写,例如 -cc-us。
部分请求出现 Read timed out(读取超时)目标网站或线路较慢捕获异常后重试。不加 -sid- 时,重试会使用新 IP。

在服务器上用 IP 白名单运行脚本

如果脚本运行在有固定 IPv4 地址的服务器上,可以在控制台把该地址加入 IP 白名单。最多可添加 10 个地址。来自这些服务器的请求无需代理密码即可连接。

无论哪种方式,都不要把代理密码写进要分享或提交的代码里。请改为从环境变量读取,一旦泄露,就在控制台生成新密码。

关于本文的问题

如何在 Python requests 中设置代理?

在请求中传入 proxies={'http': proxy, 'https': proxy},其中 proxy 是完整的代理 URL。HTTP 用 6100 端口,SOCKS5 用 socks5h 和 6200 端口。

为什么会出现 407 错误?

407 表示代理没有接受登录信息。检查用户名、参数和代理密码。代理密码不是您登录本网站的密码。

如何在 Python 中轮换 IP?

不需要额外代码。用户名中不加 -sid- 时,每个请求都会获得新 IP。只有需要保持 IP 时才加 -sid-。

同一段代码适用于所有 HiProxy 产品吗?

适用。把 -zone- 改为 res、unl、isp 或 dc 即可。主机、端口和密码都不变。

需要单独购买 SOCKS5 套餐吗?

不需要。每个套餐都可以使用两个端口,只需修改 URL 中的协议和端口。

充值 $50,获得 185 GB 住宅流量

设置用户名和密码即可开始使用。余额永不过期。