2

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?

user1417933
  • 805
  • 2
  • 11
  • 14

1 Answers1

6

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
Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • If you use this proxy from within your browser, what IP do you see when accessing this page? – Tadeck Jun 21 '12 at 01:12
  • I'm not using any proxy in my browser, and it displays the same when accessed in the browser: my normal IP. – user1417933 Jun 21 '12 at 01:15
  • 1
    @user1417933: So please set the same proxy within your browser and try again. – Tadeck Jun 21 '12 at 01:24
  • Tried, I'm still getting my original IP. – user1417933 Jun 21 '12 at 02:32
  • Okay, I removed the 's' from the https, and now it gives me the proxy IP. However, when I try visiting HTTPS pages, it sends it as my original IP. How can I resolve this? – user1417933 Jun 21 '12 at 02:34
  • It looks like the problem lies in the proxy. Go to the http://ip.42.pl/ URL and tell us which IP you see. Also see the headers if they contain any information about the proxy. The problem may be just you use transparent proxy for https. – Tadeck Jun 21 '12 at 02:36
  • I see the same result from that page: the proxy's IP when visiting via HTTP, my IP when visiting via HTTPS. Same result in the browser and script. All I know is that it's a HTTPS proxy from a public proxy list, is the issue with that and I need to find another one? – user1417933 Jun 21 '12 at 02:45
  • 1
    @user1417933: Yes, this proves that the issue lies on your proxy, not in the code you are trying to use. – Tadeck Jun 21 '12 at 03:00