带有urrlib和proxy的Python Head请求没有

2024-06-16 11:37:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我没能通过我当地的Tor代理来做头部请求

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

我收到很多错误信息,正确的语法是什么?在


Tags: importhost代理wwwderesconnhttplib
1条回答
网友
1楼 · 发布于 2024-06-16 11:37:32

你的torproxy是一个SOCKS代理,httplib不支持它。在

您可以使用requests的最新版本(无论如何,httplib建议使用它而不是它本身)。在

安装

然后,您可以:

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'}

相关问题 更多 >