python 代理類型說明
阿新 • • 發佈:2017-12-26
種類型 設置 clas lib 代理 status 出現 1.10 說明
環境 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 代理類型說明