urllib2 won't use my proxy

ぃ、小莉子 提交于 2019-12-10 11:28:51

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/11130184/urllib2-wont-use-my-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!