> For the complete documentation index, see [llms.txt](https://docs.ipwo.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ipwo.net/kai-fa-zhe-wen-dang/dai-ma-shi-li/python/requests.md).

# Requests

使用 Python `requests` 可以快速通过 [IPWO 代理](https://www.ipwo.net/)发起 HTTP 请求，适用于 API 调用、[数据采集](/ying-yong-chang-jing/shu-ju-cai-ji.md)与代理测试。

***

### 一、安装 requests

```bash
pip install requests
```

***

### 二、HTTP 代理示例

```python
import requests

if __name__ == '__main__':
    proxyip = "http://username_custom_zone_US:password@us.ipwo.net:7878"

    url = "http://ipinfo.io"

    proxies = {
        'http': proxyip,
        'https': proxyip
    }

    data = requests.get(
        url=url,
        proxies=proxies
    )

    print(data.text)
```

***

### 三、正常返回示例

```json
{
  "ip": "203.0.113.10",
  "country": "US"
}
```

如果返回的 IP：

* 不是本机公网 IP
* 国家地区正确

说明代理已经生效。

***

### 四、增加 timeout（推荐）

建议开发者增加 timeout：

```python
data = requests.get(
    url=url,
    proxies=proxies,
    timeout=30
)
```

避免请求长时间卡住。

***

### 五、常见问题

***

#### [407 Proxy Authentication Required](/kai-fa-zhe-wen-dang/chang-jian-cuo-wu/407-proxy-authentication-required.md)

通常是：

* 用户名错误
* 密码错误
* zone 参数错误

***

#### [Timeout 超时](/kai-fa-zhe-wen-dang/chang-jian-cuo-wu/chao-shi-timeout.md)

通常是：

* 网络环境异常
* 国内网络限制
* DNS 问题

***

#### IP 未变化

请检查：

* 是否正确使用 proxies
* 是否请求真正走代理
* 是否本地缓存连接

***

### 六、推荐开发建议

推荐：

* 使用[<kbd>海外 VPS</kbd>](/huan-jing-zhun-bei/vps-yun-fu-wu-qi-tui-jian.md)
* 增加 timeout
* 增加 Retry 重试机制
* 使用粘性 Session

***

### 七、相关文章

* [cURL](/kai-fa-zhe-wen-dang/dai-ma-shi-li/curl-dai-ma-shi-li.md)
* [如何检测代理是否生效](/dai-li-jian-ce-yu-yin-si-an-quan/ru-he-jian-ce-dai-li-shi-fou-sheng-xiao.md)
* [407 Proxy Authentication Required](/kai-fa-zhe-wen-dang/chang-jian-cuo-wu/407-proxy-authentication-required.md)
* [超时 Timeout](/kai-fa-zhe-wen-dang/chang-jian-cuo-wu/chao-shi-timeout.md)
* [什么是粘性会话](/dai-li-ji-chu-zhi-shi/shen-me-shi-nian-xing-hui-hua.md)
* [API 调用与批量采集](/dai-li-ji-chu-zhi-shi/api-diao-yong-yu-pi-liang-cai-ji.md)
