# ตัวอย่างโค้ดพร็อกซีดาต้าเซ็นเตอร์: curl, Python, Node.js และ Playwright

> ตัวอย่างโค้ดพร็อกซีสำหรับ curl, Python requests และ aiohttp, Node.js undici และ Playwright, HTTP และ SOCKS5 RackProxies: เริ่มต้นที่ US$1.29 ต่อ IP, ไม่ต้อง KYC, จ่ายด้วยคริปโต

- URL: https://rackproxies.com/th/docs/code-examples/
- อัปเดตล่าสุด: 2026-10-19
- Publisher: RackProxies (https://rackproxies.com)

โค้ดที่พร้อมคัดลอกสำหรับไคลเอนต์ที่ใช้บ่อยที่สุด แต่ละตัวอย่างผ่านการทดสอบไวยากรณ์ ทุกตัวอย่างชี้ไปที่ api.ipify.org, httpbin.org/ip หรือ example.com

**คำตอบสั้น — ฉันจะใช้พร็อกซีดาต้าเซ็นเตอร์ในโค้ดได้อย่างไร?**

ตัวอย่างโค้ดพร็อกซีเหล่านี้ส่ง URL `http://USERNAME:PASSWORD@IP:PORT` หนึ่งรายการไปยังไคลเอนต์ของคุณ: `-x` ใน curl, `proxies=` ใน Python requests, `ProxyAgent` ใน Node.js, ตัวเลือก `proxy` ใน Playwright SOCKS5 ใช้ `socks5h://` **RackProxies** IP รองรับ HTTP, HTTPS (CONNECT) และ SOCKS5 เริ่มต้นที่ US$1.29 ต่อ IPv4 เฉพาะเป็นเวลา 30 วัน ไม่ต้อง KYC จ่ายด้วยคริปโตเท่านั้น

## ฉันต้องใช้โค้ดตัวอย่างไหน?

*ที่อยู่เอกสาร 203.0.113.24 และพอร์ต 8000 ในทุกโค้ดตัวอย่าง: แทนที่ด้วยบรรทัดจากบัญชีของคุณ*

| ไคลเอนต์ | พร็อกซี HTTP | พร็อกซี SOCKS5 | แพ็กเกจเพิ่มเติม |
| --- | --- | --- | --- |
| curl | `-x http://…` | `-x socks5h://…` | ไม่มี |
| Python requests | `proxies={…}` | URL `socks5h://` | `requests[socks]` สำหรับ SOCKS5 |
| Python aiohttp | `proxy=` และ `proxy_auth=` | ไม่มีในตัว | `aiohttp-socks` สำหรับ SOCKS5 |
| Node.js fetch | undici `ProxyAgent` | ไม่มีในตัว | `undici` |
| Node.js https | `https-proxy-agent` | `socks-proxy-agent` | หนึ่งในสอง |
| Playwright | `proxy: { server, username, password }` | ใช้ HTTP หรือ allowlist | `playwright` |

## ฉันจะใช้พร็อกซีกับ curl ได้อย่างไร?

ส่ง URL พร็อกซีด้วย `-x` curl จัดการอุโมงค์ CONNECT สำหรับเป้าหมาย `https://` ด้วยตัวเอง ทุกโค้ดตัวอย่างในหน้านี้ใช้ที่อยู่เอกสาร `203.0.113.24`; หน้า [รูปแบบการเชื่อมต่อ](https://rackproxies.com/th/docs/connection-format/) แสดงวิธีสร้าง URL จากบรรทัดของคุณ

*curl: HTTP, SOCKS5, ล็อกอินแยก, allowlist*

```bash
# HTTP proxy, HTTPS target (CONNECT tunnel)
curl -x http://USERNAME:PASSWORD@203.0.113.24:8000 https://api.ipify.org

# SOCKS5, hostname resolved by the proxy
curl -x socks5h://USERNAME:PASSWORD@203.0.113.24:8000 https://api.ipify.org

# Login passed separately (no URL-encoding needed)
curl -x http://203.0.113.24:8000 -U 'USERNAME:PASSWORD' https://httpbin.org/ip

# IP allowlist: no login
curl -x http://203.0.113.24:8000 https://api.ipify.org

# Show the proxy handshake and the timings
curl -v -s -o /dev/null -w '%{time_connect} %{time_appconnect} %{time_total}\n' \
  -x http://USERNAME:PASSWORD@203.0.113.24:8000 https://example.com
```

## ฉันจะใช้พร็อกซีกับ Python requests ได้อย่างไร?

ให้ requests พจนานุกรม `proxies` ที่มี URL เดียวกันสำหรับ `http` และ `https` คีย์ `https` คือคีย์ที่ใช้สำหรับเป้าหมาย `https://`; ค่าของมันยังคงเริ่มต้นด้วย `http://` เพราะมันอธิบายการเชื่อมต่อไปยังพร็อกซี requests จะเกิดข้อผิดพลาด `ProxyError` เมื่อพร็อกซีปฏิเสธการเชื่อมต่อหรือการล็อกอิน

*requests ผ่านพร็อกซี HTTP พร้อมการจัดการข้อผิดพลาด*

```python
import requests  # pip install requests

# From the line 203.0.113.24:8000:USERNAME:PASSWORD in your account
proxy = 'http://USERNAME:PASSWORD@203.0.113.24:8000'
proxies = {'http': proxy, 'https': proxy}

try:
    response = requests.get('https://api.ipify.org', proxies=proxies, timeout=20)
    response.raise_for_status()
    print('Exit IP:', response.text)
except requests.exceptions.ProxyError as error:
    print('Proxy problem (credentials, port or allowlist):', error)
except requests.exceptions.Timeout:
    print('No answer within 20 seconds')
```

*คำขอผ่าน SOCKS5 (pip install "requests[socks]")*

```python
import requests  # pip install "requests[socks]"

# socks5h: the proxy resolves the hostname. Use the SOCKS5 port shown in your account.
proxy = 'socks5h://USERNAME:PASSWORD@203.0.113.24:8000'
proxies = {'http': proxy, 'https': proxy}

response = requests.get('https://httpbin.org/ip', proxies=proxies, timeout=20)
print(response.json())  # {'origin': '203.0.113.24'}
```

*Session สำหรับหลายคำขอผ่าน IP เดียว*

```python
import requests

# A Session reuses connections: faster for many requests through the same IP.
session = requests.Session()
session.proxies.update({
    'http': 'http://USERNAME:PASSWORD@203.0.113.24:8000',
    'https': 'http://USERNAME:PASSWORD@203.0.113.24:8000',
})
session.headers['User-Agent'] = 'my-monitor/1.0 (contact: you@example.com)'

for url in ['https://example.com', 'https://httpbin.org/ip']:
    response = session.get(url, timeout=20)
    print(url, response.status_code, len(response.content), 'bytes')
```

ตั้งค่า `User-Agent` ที่ระบุชื่อเครื่องมือและช่องทางติดต่อ ดังในตัวอย่าง Session: เจ้าของไซต์จะติดต่อคุณได้แทนการบล็อก requests ยังอ่าน environment variable `HTTP_PROXY` และ `HTTPS_PROXY` ด้วยเมื่อไม่ได้ระบุอาร์กิวเมนต์ `proxies`

## ฉันจะใช้พร็อกซีกับ Python aiohttp ได้อย่างไร

aiohttp รับพร็อกซีต่อคำขอ: `proxy=` สำหรับที่อยู่ และ `proxy_auth=` สำหรับการเข้าสู่ระบบ รองรับพร็อกซี HTTP รวมถึง CONNECT สำหรับเป้าหมาย `https://` สำหรับ SOCKS5 ต้องใช้แพ็กเกจ `aiohttp-socks` แยกต่างหาก

*aiohttp ผ่านพร็อกซี HTTP*

```python
import asyncio

import aiohttp  # pip install aiohttp (HTTP proxies; SOCKS needs the aiohttp-socks package)

PROXY = 'http://203.0.113.24:8000'
AUTH = aiohttp.BasicAuth('USERNAME', 'PASSWORD')


async def main():
    timeout = aiohttp.ClientTimeout(total=20)
    async with aiohttp.ClientSession(timeout=timeout) as session:
        async with session.get('https://httpbin.org/ip', proxy=PROXY, proxy_auth=AUTH) as response:
            print(await response.json())


asyncio.run(main())
```

## ฉันจะใช้พร็อกซีกับ Node.js ได้อย่างไร

`fetch` ที่มีมาใน Node ไม่รับอาร์กิวเมนต์พร็อกซี ให้ใช้แพ็กเกจ `undici` และ `ProxyAgent` เป็น `dispatcher` สำหรับโมดูล `https` แบบคลาสสิก ให้ใช้ `https-proxy-agent` สำหรับพร็อกซี HTTP และ `socks-proxy-agent` สำหรับ SOCKS5 ทั้งสองตัวอย่างเป็น ES module: บันทึกเป็นไฟล์ `.mjs`

*fetch ด้วย undici ProxyAgent*

```javascript
// npm install undici
import { ProxyAgent, fetch } from 'undici';

const dispatcher = new ProxyAgent({
  uri: 'http://203.0.113.24:8000',
  token: `Basic ${Buffer.from('USERNAME:PASSWORD').toString('base64')}`,
});

const response = await fetch('https://api.ipify.org', { dispatcher });
console.log('Exit IP:', await response.text());
```

*https ด้วย https-proxy-agent และ socks-proxy-agent*

```javascript
// npm install https-proxy-agent socks-proxy-agent
import https from 'node:https';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { SocksProxyAgent } from 'socks-proxy-agent';

const httpAgent = new HttpsProxyAgent('http://USERNAME:PASSWORD@203.0.113.24:8000');
// Use the SOCKS5 port shown in your account; socks5h lets the proxy resolve DNS.
const socksAgent = new SocksProxyAgent('socks5h://USERNAME:PASSWORD@203.0.113.24:8000');

for (const agent of [httpAgent, socksAgent]) {
  https.get('https://api.ipify.org', { agent }, (res) => {
    res.setEncoding('utf8');
    res.on('data', (chunk) => console.log('Exit IP:', chunk));
  }).on('error', (err) => console.error('Proxy error:', err.message));
}
```

## ฉันจะใช้พร็อกซีกับ Playwright ได้อย่างไร

ส่งออบเจกต์ `proxy` ให้ `chromium.launch()` พร้อม server และแยก username กับ password ใช้อแบบ HTTP: ไคลเอนต์ SOCKS5 ที่มีมาใน Chromium ไม่รองรับการยืนยันตัวตนด้วย username และ password สำหรับพร็อกซี SOCKS5 หากใช้ allowlist ของ IP ให้ละเว้น `username` และ `password`

*Playwright (Chromium) ผ่านพร็อกซี HTTP*

```javascript
// npm install playwright && npx playwright install chromium
import { chromium } from 'playwright';

const browser = await chromium.launch({
  proxy: { server: 'http://203.0.113.24:8000', username: 'USERNAME', password: 'PASSWORD' },
});
const page = await browser.newPage();
await page.goto('https://api.ipify.org');
console.log('Exit IP:', await page.textContent('body'));
await browser.close();
```

ใช้เบราว์เซอร์ผ่านพร็อกซีสำหรับ QA ไซต์ของคุณเอง การตรวจสอบโฆษณาและการแปล และงานที่ถูกกฎหมายในลักษณะเดียวกัน โปรดจำไว้ว่าพร็อกซีเปลี่ยนเฉพาะ IP: ภาษาและเขตเวลาของเบราว์เซอร์ยังคงเป็นของเครื่องคุณ เว้นแต่คุณจะตั้งค่าในบริบทของเบราว์เซอร์

## ฉันจะวัด latency ผ่านพร็อกซีได้อย่างไร

จับเวลาคำขอที่สมบูรณ์หลายครั้งแล้วใช้ค่ามัธยฐาน สคริปต์ Python ด้านล่างส่งห้าคำขอไปยัง `https://example.com` ผ่าน IP เดียว รันผ่าน IP ในเมืองศูนย์ข้อมูลต่างๆ เพื่อเปรียบเทียบ [คู่มือเมือง](https://rackproxies.com/th/guides/choose-a-datacenter-city/) อธิบายพื้นทางกายภาพสำหรับแต่ละระยะทาง

*เวลาคำขอค่ามัธยฐานผ่านพร็อกซี*

```python
import statistics
import time

import requests

proxy = 'http://USERNAME:PASSWORD@203.0.113.24:8000'
timings = []
for _ in range(5):
    start = time.perf_counter()
    requests.get('https://example.com', proxies={'http': proxy, 'https': proxy}, timeout=20)
    timings.append((time.perf_counter() - start) * 1000)

print(f'median {statistics.median(timings):.0f} ms over {len(timings)} requests')
```

## ฉันจะใช้รายการ IP ทั้งหมดได้อย่างไร

โหลดไฟล์ TXT ที่ส่งออก แปลงแต่ละบรรทัดเป็น URL และเลือก IP ต่อคำขอ: หมุนเวียนข้ามไซต์ หรือใช้ IP เดียวกันสำหรับไซต์เดียวกัน [คู่มือ rotating กับ static](https://rackproxies.com/th/guides/rotating-vs-static-datacenter-proxies/) มีตัวอย่างครบถ้วน และหน้า [รูปแบบการเชื่อมต่อ](https://rackproxies.com/th/docs/connection-format/) มีตัวแปลงสำหรับทั้งรายการ เคารพจังหวะของแต่ละไซต์ไม่ว่าคุณจะมีรายการขนาดใด: IP ที่มากขึ้นไม่ได้เพิ่มขีดจำกัดของไซต์

## คำถามเกี่ยวกับตัวอย่างโค้ด

### ทำไมฉันใช้ http:// สำหรับพร็อกซีเมื่อไซต์เป็น https://?

scheme ของ URL พร็อกซีอธิบายการเชื่อมต่อของคุณกับพร็อกซี ไม่ใช่กับเว็บไซต์ ไคลเอนต์ของคุณเชื่อมต่อกับพร็อกซีผ่าน TCP ธรรมดา ขอให้เปิดทันเนล CONNECT แล้วจึงรัน TLS กับเว็บไซต์ภายใน เนื้อหาหน้าจะยังเข้ารหัสแบบ end to end; มีเพียงการเข้าสู่ระบบพร็อกซีที่เดินทางในรูปแบบที่อ่านได้

### รหัสผ่านของฉันมีอักขระพิเศษ มีอะไรเปลี่ยนแปลง?

ในรูปแบบ URL ให้เข้ารหัสรหัสผ่านด้วย `urllib.parse.quote(password, safe='')` ใน Python หรือ `encodeURIComponent(password)` ใน JavaScript ไคลเอนต์ที่รับการเข้าสู่ระบบแยกต่างหาก เช่น `BasicAuth` ของ aiohttp, `username` และ `password` ของ Playwright หรือ `-U` ของ curl ไม่ต้องเข้ารหัส หน้า [รูปแบบการเชื่อมต่อ](https://rackproxies.com/th/docs/connection-format/) แสดงรายการอักขระ

### ทำไม requests บอกว่าขาด dependency สำหรับการรองรับ SOCKS?

การรองรับ SOCKS ของ requests อยู่ใน extra แบบเลือกติดตั้ง ติดตั้งด้วย `pip install "requests[socks]"` ซึ่งจะเพิ่ม PySocks และใช้ URL `socks5h://` เพื่อให้พร็อกซีแปลงชื่อโฮสต์ พร็อกซี HTTP ทำงานได้โดยไม่ต้องมีแพ็กเกจเพิ่ม所以你สามารถเปลี่ยน URL เป็น `http://` และเก็บ dependency เดิมไว้ได้

### ฉันสามารถรันคำขอจำนวนมากแบบขนานผ่าน IP เดียวได้หรือไม่

ได้ ภายในการใช้งานอย่างเป็นธรรม เราไม่เผยแพร่ขีดจำกัดการเชื่อมต่อต่อ IP รักษาระดับการทำงานขนานให้เหมาะสมเพื่อความเสถียรของคุณเอง และรักษาจังหวะของแต่ละเว็บไซต์: คำขอแบบขนานไปยังไซต์เดียวกันยังนับรวมกับขีดจำกัดของไซต์นั้น คิดค่าบริการเป็นราย IP 30 วัน ไม่ใช่ราย GB ภายใต้นโยบายการใช้งานอย่างเป็นธรรม

### โค้ดของฉันควรเรียก URL ทดสอบใดก่อน

เรียก `https://api.ipify.org` หรือ `https://httpbin.org/ip`: แต่ละตัวจะคืนค่า IP ที่มองเห็น เพื่อให้คุณยืนยันได้ว่าทราฟฟิกออกจากพร็อกซีของคุณ จากนั้นเรียกเป้าหมายจริงของคุณ หาก URL ทดสอบทำงานแต่เป้าหมายไม่ทำงาน ปัญหาอยู่ระหว่างพร็อกซีกับเป้าหมาย ไม่ใช่ในโค้ดของคุณ

### ฉันต้องใช้บัตรประชาชนเพื่อรับพร็อกซีสำหรับตัวอย่างเหล่านี้หรือไม่

ไม่ต้อง ไม่ต้อง KYC: ไม่ต้องใช้เอกสารระบุตัวตน ไม่ต้องเซลฟี่ ไม่ต้องลงนามเอกสารใด ๆ เพื่อสมัคร เติมเงิน หรือซื้อ เติมเงินใน BTC, USDT, USDC, ETH, SOL, LTC ซื้อแพ็กเกจจาก US$1.29 และคัดลอกบรรทัดจากบัญชีของคุณไปยังตัวอย่างโค้ด ดู [ราคา](https://rackproxies.com/th/pricing/) สำหรับทุกขนาดแพ็กเกจ

**รันตัวอย่างด้วย IP ของคุณเอง** 1 IP สำหรับ US$1.29 รวม HTTP และ SOCKS5 ไม่ต้อง KYC จ่ายด้วยคริปโต [ดูราคา](https://rackproxies.com/th/pricing/) · [เริ่มต้นอย่างรวดเร็ว](https://rackproxies.com/th/docs/quickstart/)

## ข้อมูลอ้างอิง

1. [หน้า man ของ curl](https://curl.se/docs/manpage.html) — curl project (2026-10-19)
2. [Requests: พร็อกซีและ SOCKS](https://requests.readthedocs.io/en/latest/user/advanced/) — Requests (2026-10-19)
3. [aiohttp: การรองรับพร็อกซี](https://docs.aiohttp.org/en/stable/client_advanced.html) — aiohttp (2026-10-19)
4. [undici: ProxyAgent](https://undici.nodejs.org/) — Node.js (2026-10-19)
5. [proxy-agents (https-proxy-agent, socks-proxy-agent)](https://github.com/TooTallNate/proxy-agents) — GitHub (2026-10-19)
6. [Playwright: พร็อกซี HTTP](https://playwright.dev/docs/network) — Playwright (2026-10-19)

---

RackProxies: RackProxies เป็นผู้ให้บริการพร็อกซีดาต้าเซ็นเตอร์แบบเฉพาะ: ที่อยู่ IPv4 เช่าเป็นราย IP 30 วัน จ่ายด้วยคริปโต ไม่ต้องอัปโหลดเอกสารระบุตัวตน หน้าเว็บครอบคลุม 22 ประเทศและ 34 เมืองของดาต้าเซ็นเตอร์ พร้อมข้อมูลเครือข่ายสาธารณะ
