码迷,mamicode.com
首页 > 编程语言 > 详细

python 代理类型说明

时间:2017-12-26 00:55:18      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:https   选择   响应   开头   pos   情况   失败   14.3   log   

环境 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 都能代理成功了。

python 代理类型说明

标签:https   选择   响应   开头   pos   情况   失败   14.3   log   

原文地址:https://www.cnblogs.com/wisdom3/p/8111599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!