请求+代理服务器,IP地址不会改变

Requests + Proxy Servers, IP address won't change

我正在使用 python shell 与代理服务器一起测试请求。 在阅读文档 (http://docs.python-requests.org/en/master/user/advanced/) 和几个 Whosebug 线程后,我正在执行以下操作:

import requests

s = requests.session()
proxies = {'http': 'http://90.178.216.202:3128'}
s.proxies.update(proxies)
req = s.get('http://jsonip.com')

在此之后,如果我打印 req.text,我会得到: u'{"ip":"my current IP (not the proxy server IP I have inserted before)","about":"/about", ......}'

你能解释一下为什么我得到的是我电脑的 IP 地址而不是代理服务器的 IP 地址吗? 我是在某个地方出错了,还是我预料到这里会发生错误的事情? 我是请求 + 代理服务器的新手,所以我想确保我理解这一点。

更新 我的代码中也有这个:

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0'}
s.headers.update(headers)

谢谢 维托里奥

而不是这样做通过用户代理

requests.post(url='abc.com',header={'user-agent':'Mozila 5.0'})

站点 ( http://jsonip.com ) 广播了一个 'Upgrade-Insecure-Requests' header。这意味着您的请求被重定向到 https://jsonip.com,因此 requests 不使用代理,因为您没有 https 代理你的 proxies 字典。

因此,您所要做的就是在 proxies 中添加一个 https 代理,例如:

proxies = {'http':'http://90.178.216.202:3128', 'https':'https://90.178.216.202:3128'}

您需要更改获取请求以使用代理。

像这样:req = s.get('http://jsonip.com', proxies=proxies)