[英]Python3: http.client with privoxy/TOR making bad requests
[英]How to get HTTPS content using Python Requests through TOR and Privoxy
我在我的系統上使用Privoxy進行了TOR設置,經過測試並運行良好。 我想要做的是通過此設置代理HTTPS請求,以便這些GET和POST通過TOR。 以下是我可以生成的最簡單的代碼版本:
import requests
proxy = { 'http':'127.0.0.1:8118','https':'127.0.0.1:8118' }
r = requests.get('https://www.whatismyip.com/',proxies=proxy)
#r = requests.get('http://www.whatsmyip.org/')
print r
使用HTTPS時,我沒有獲得響應正文(r.content為空),但我確實獲得了200狀態代碼,我可以在Privoxy日志中看到請求。 我在這個帖子中看到了一個錯誤,但似乎幾個月前在Requests庫中解決了這個問題 。
我的privoxy設置是基本的設置,並通過添加以下兩行來監聽localhost:
forward-socks4a / localhost:9050 .
forward-socks5 / localhost:9050 .
在這一點上,我不確定發生了什么,但我做的事似乎沒有用。 我在Python 2.6.5上使用最新的請求庫和urllib3。
urllib3
尚未正確支持HTTPS代理(由requests
)。
有關詳細信息,請參見請求問題905和urllib3問題50 。
嘗試requesocks
,一個設計用於直接連接到SOCKS代理的requests
的分支(就像Tor一樣,首先沒有使用Privoxy。如果你需要Privoxy,你可能會運氣不好,但除此之外, pip install requesocks
並替換
import requests
同
import requesocks
requests = requesocks.session()
requests.proxies = {"http": "socks5://127.0.0.1:8118",
"https": "socks5://127.0.0.1:8118"}
現在, requests
變量是一個會話(帶有requests.get()
和friends),您可以使用與直接導入requests
完全相同的方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.