当前位置:首页 > Python教程 > python技巧

python 代理类型说明

环境 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


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐