python 代理类型说明

本文介绍了Python中使用requests和urllib库进行代理设置的方法,并详细解释了HTTP与HTTPS代理的区别及正确配置方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境 python 2.7

python 代理类型选择

python代理设置通常如下:

proxies = {
  "http": "http://10.10.1.10:3128",
  "https": "http://10.10.1.10:1080",
}

在HTTP 和 HTTPS 两种类型中,HTTPS类型的ip必须对应如:"https" : "https...", 像如:"http" : "https..."这样设置是错误的。

测试代码实例,主要以下有4种情况:

1.requests库代理代码入下:

import requests

proxies = {"http" : "http://122.114.31.177:808"}  # 1. 成功
proxies = {"http" : "https://110.73.50.236:8123"} # 2. 失败
proxies = {"https" : "http://122.114.31.177:808"}  #  3. 成功
proxies = {"https" : "https://110.73.50.236:8123"} #  4. 失败

response = requests.get("http://www.baidu.com", proxies=proxies)
print response.status_code # 检测响应

2.urllib库代理代码如下:

import urllib

proxies = {"http" : "http://122.114.31.177:808"}  # 1. 成功
proxies = {"http" : "https://110.73.50.236:8123"} # 2. 失败
proxies = {"https" : "http://122.114.31.177:808"}  #  3. 成功
proxies = {"https" : "https://110.73.50.236:8123"} #  4. 失败

response = urllib.urlopen("http://www.baidu.com",proxies=proxies)
print response.getcode() # 检测响应

总结

为了避免出现错误,通常我们只需在开头设置HTTPS 代理类型,这样HTTP和HTTP类型的ip 都能代理成功了。

转载于:https://www.cnblogs.com/wisdom3/p/8111599.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值