开发者在接入 MaskProxy 时,建议先写一个最小可运行示例,确认代理主机、端口、认证方式和出口 IP 都正确,再接入正式业务代码。
Python requests 示例
import requests
proxy = "http://用户名:密码@主机:端口"
proxies = {
"http": proxy,
"https": proxy,
}
resp = requests.get("https://api.ipify.org?format=json", proxies=proxies, timeout=20)
print(resp.status_code)
print(resp.text)
如果你的任务需要更高稳定性,可以加入重试、超时和异常处理。不要把代理账号密码直接提交到代码仓库,建议从环境变量读取。
Node.js 示例
Node.js 原生 fetch 本身不直接处理所有代理场景,建议使用成熟的代理 Agent 库。下面示例展示基本思路:
import fetch from "node-fetch";
import { HttpsProxyAgent } from "https-proxy-agent";
const proxyUrl = "http://用户名:密码@主机:端口";
const agent = new HttpsProxyAgent(proxyUrl);
const res = await fetch("https://api.ipify.org?format=json", { agent });
console.log(await res.text());
安全建议
- 把代理地址、用户名和密码放在环境变量或安全配置中。
- 给请求设置合理超时时间,避免任务长时间卡住。
- 先用小批量请求测试,再逐步增加并发。
当最小示例可以稳定返回出口 IP 后,再把同样的代理配置迁移到你的爬虫、自动化脚本或数据采集流程中。