Python and Node.js Proxy Examples

When developers access MaskProxy, it is recommended to write a minimum runnable example first, confirm that the proxy host, port, authentication method and exit IP are correct, and then access the formal business code.

Python requests example

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)

If your task requires higher stability, you can add retries, timeouts, and exception handling. Do not submit the proxy account and password directly to the code repository. It is recommended to read it from environment variables.

Node.js Example

Node.js native fetch itself does not directly handle all proxy scenarios, and it is recommended to use a mature proxy library. The following example shows the basic idea:

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());

Security Recommendation

  • Put the proxy address, username and password in environment variables or security configuration.
  • Set a reasonable timeout for requests to avoid tasks getting stuck for a long time.
  • Test with small batch requests first, and then gradually increase concurrency.

After the minimal example can stably return the exit IP, migrate the same proxy configuration to your crawler, automated script or data collection process.

Powered by MaskProxy