urllib2 不会使用我的代理 [英] urllib2 won't use my proxy

查看:70
本文介绍了urllib2 不会使用我的代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我用 HTTPS 代理构建的打开器打开带有 urllib2 的 URL,但是它使用我的普通 IP 请求它,而不是我给它的代理.

I'm trying to open a URL with urllib2 using an opener I built with a HTTPS proxy, however it is requesting it with my normal IP, and not the proxy I give it.

import urllib2

proxy  = urllib2.ProxyHandler({'https': 'IP:PORT'})
opener = urllib2.build_opener(proxy)

my_ip = opener.open('http://whatthehellismyip.com/?ipraw').read()
print my_ip

谁能告诉我我在这里做错了什么?

Can anyone please tell me what I am doing wrong here?

推荐答案

您忘记安装 opener.这应该有效:

You forgot to install opener. This should work:

import urllib2

proxy  = urllib2.ProxyHandler({'https': 'IP:PORT'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)

my_ip = urllib2.urlopen('http://whatthehellismyip.com/?ipraw').read()
print my_ip

这篇关于urllib2 不会使用我的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆