簡體   English   中英

帶有urrlib和代理的Python Head請求不起作用

[英]Python Head request with urrlib and proxy doesn't work

我無法通過本地Tor代理執行HEAD請求

import httplib
host = 'www.heise.de'
inputfilename="/newsticker/classic/"

conn = httplib.HTTPSConnection("127.0.0.1", 9151)
conn.set_tunnel(host, 443)
conn.request("HEAD", inputfilename)
res = conn.getresponse()

print res

我收到很多錯誤消息,正確的語法是什么?

您的Tor代理是SOCKS代理,httplib不支持。

您可以使用最新版本的requests (無論如何, httplib建議使用該版本而不是其本身)。

安裝requestspySocks

然后,您可以執行以下操作:

import requests
proxies = {
    'http': 'socks5://127.0.0.1:9050',
    'https': 'socks5://127.0.0.1:9050'
}

# You need to use the url, not just the host name
url = 'http://www.heise.de'
response = requests.head(url, proxies=proxies)
print(response.headers)

#{'Vary': 'X-Forwarded-Proto, ... 'Last-Modified': 'Sun, 26 Feb 2017 09:27:45 GMT'}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM