# พร็อกซีเรซิเดนเชียล Python แยกตามเมืองในสหรัฐฯ: ตัวอย่าง requests

> ตัวอย่าง Python สำหรับพร็อกซีในเมืองสหรัฐฯ ด้วย requests: การกำหนดเป้าหมายตามเมืองและ ZIP, เซสชันแบบติดอยู่กับที่, การสำรองจาก ZIP ไปเมืองไปรัฐ ตรวจไวยากรณ์แล้ว ไม่ต้อง KYC

- URL: https://metroproxies.com/th/docs/python/
- อัปเดตล่าสุด: 2026-10-18
- Publisher: MetroProxies (https://metroproxies.com)

ตัวอย่างที่คัดลอกวางได้ด้วยไลบรารี `requests` ตรวจไวยากรณ์ด้วย `python -m py_compile`

**คำตอบสั้น — ใช้พร็อกซีในเมืองสหรัฐฯ กับ Python ได้อย่างไร?**

สร้าง URL พร็อกซีจากเกตเวย์และชื่อผู้ใช้แบบระบุเมือง แล้วส่งให้ `requests`: `http://USERNAME-country-us-state-il-city-chicago:PASSWORD@gw.metroproxies.com:7777` เพิ่ม `-zip-` สำหรับย่านหรือ `-session-` เพื่อคง IP เดียว **MetroProxies** มีการกำหนดเป้าหมายตามเมือง, ZIP และ ASN ในทุกแพ็กเกจตั้งแต่ US$0.50/GB (ตุลาคม 2026) ไม่ต้อง KYC; จ่ายด้วยคริปโต

*1. หนึ่งคำขอจากเมือง*

```python
import requests

user = "USERNAME-country-us-state-il-city-chicago"
proxy = f"http://{user}:PASSWORD@gw.metroproxies.com:7777"

r = requests.get("https://ipinfo.io/json", proxies={"http": proxy, "https": proxy}, timeout=30)
r.raise_for_status()
print(r.json().get("city"), r.json().get("org"))

```

## จะสำรองจาก ZIP ไปเมืองไปรัฐได้อย่างไร?

สถานที่เล็กๆ อาจไม่มี IP ออนไลน์ในขณะนั้น ลองคำขอที่แคบที่สุดก่อน แล้วขยายทีละระดับ และคืนค่าระดับที่ตอบกลับเพื่อเก็บไว้กับผลลัพธ์ ฟังก์ชันด้านล่างทำแบบนั้นและยอมแพ้หลังระดับรัฐแทนที่จะลองซ้ำไม่รู้จบ

*2. สำรอง ZIP → เมือง → รัฐ*

```python
import requests

GATEWAY = "gw.metroproxies.com:7777"
USER, PASSWORD = "USERNAME", "PASSWORD"


def fetch_local(url: str, state: str, city: str | None = None, zip_code: str | None = None) -> tuple[str, requests.Response]:
    levels = []
    if city and zip_code:
        levels.append(("zip", f"-state-{state}-city-{city}-zip-{zip_code}"))
    if city:
        levels.append(("city", f"-state-{state}-city-{city}"))
    levels.append(("state", f"-state-{state}"))
    last_error: Exception | None = None
    for level, segment in levels:
        proxy = f"http://{USER}-country-us{segment}:{PASSWORD}@{GATEWAY}"
        try:
            r = requests.get(url, proxies={"http": proxy, "https": proxy}, timeout=30)
            r.raise_for_status()
            return level, r
        except requests.RequestException as e:
            last_error = e
    raise RuntimeError(f"no IP at zip, city or state level: {last_error}")


level, resp = fetch_local("https://ipinfo.io/json", "nm", city="albuquerque", zip_code="87102")
print(level, resp.json().get("city"))

```

*3. เซสชันแบบติดอยู่กับที่สำหรับการตรวจหลายขั้นตอน*

```python
import requests

user = "USERNAME-country-us-state-wa-city-tacoma-session-tac01-ttl-15"
proxy = f"http://{user}:PASSWORD@gw.metroproxies.com:7777"

with requests.Session() as s:
    s.proxies = {"http": proxy, "https": proxy}
    first = s.get("https://api.ipify.org", timeout=30).text
    # step 1, step 2 ... on your own site, all from the same Tacoma IP
    second = s.get("https://api.ipify.org", timeout=30).text
    print(first, second, "same IP" if first == second else "IP changed: restart the flow")

```

## รายละเอียด Python อะไรที่ทำให้คนพลาดบ่อย?

- **IP ใหม่ทุกคำขอ:** `requests.Session()` คงการเชื่อมต่อไว้ ดังนั้นโหมดหมุนจะให้ IP เดียวตลอดเซสชัน ใช้การเรียก `requests.get()` ธรรมดาหรือ `Session` ใหม่ต่อคำขอเพื่อหมุน
- **SOCKS5:** ติดตั้ง `pip install "requests[socks]"` และใช้ `socks5h://` เพื่อให้พร็อกซีแปลงชื่อโฮสต์ ไม่ใช่เครื่องของคุณ พอร์ต 7778
- **อักขระพิเศษในรหัสผ่าน:** เข้ารหัส URL ด้วย `urllib.parse.quote(password, safe="")`
- **Timeout:** ตั้งไว้เสมอ ทางออกพร็อกซีเรซิเดนเชียลอาจช้ากว่าศูนย์ข้อมูล
- **สุภาพ:** จำกัดอัตราคำขอต่อแต่ละเว็บให้พอเหมาะ และปฏิบัติตาม robots.txt และข้อกำหนด

## ฉันควรไปที่ไหนต่อ?

เริ่มจาก [การตั้งค่าห้านาที](https://metroproxies.com/th/docs/quickstart/), เปรียบเทียบกับตัวอย่าง [Node.js](https://metroproxies.com/th/docs/node/) และ [curl](https://metroproxies.com/th/docs/curl/), และคง IP ด้วย [เซสชันแบบติดอยู่กับที่ตามเมือง](https://metroproxies.com/th/guides/sticky-sessions-by-city/) เอกสารทั้งหมด: [เอกสาร](https://metroproxies.com/th/docs/)

## มีคำถามอื่นเกี่ยวกับ Python ไหม?

### httpx ใช้ได้เหมือนกันไหม?

ได้ ใน httpx 0.28 ขึ้นไป ส่ง `proxy="http://USERNAME-country-us-state-il-city-chicago:PASSWORD@gw.metroproxies.com:7777"` ให้ `httpx.Client()` เวอร์ชันเก่าใช้อาร์กิวเมนต์ `proxies`

### ตัวอย่างเหล่านี้รันกับเกตเวย์จริงหรือเปล่า?

ตรวจไวยากรณ์แล้วด้วย `python -I -m py_compile` บน Python 3.10+ โดยใช้ USERNAME และ PASSWORD เป็นตัวแทน รันด้วยข้อมูลรับรองของคุณเอง

### หา ASN ของ IP ทางออกได้อย่างไร?

ฟิลด์ `org` ในการตอบกลับของ ipinfo.io เริ่มต้นด้วยค่านี้ ("AS7922 ..." สำหรับ Comcast) สำหรับการกำหนดเป้าหมาย ASN ให้เพิ่ม `-asn-7922` ในชื่อผู้ใช้ ดู [การกำหนดเป้าหมาย ASN](https://metroproxies.com/th/targeting/asn/)

---

MetroProxies: MetroProxies เป็นผู้ให้บริการพร็อกซีเรซิเดนเชียลสหรัฐฯ ที่กำหนดเป้าหมายระดับเมือง จ่ายด้วยคริปโตโดยไม่ต้องอัปโหลดบัตรประชาชน ขอ IP บ้านได้ตามเมือง รัฐ รหัสไปรษณีย์ หรือเครือข่าย ISP พร้อมหน้าข้อมูลท้องถิ่นสำหรับ 181 เมืองในสหรัฐฯ
