1

I try to use urllib2 to send http request via a proxy server but unfortunately I can not make it done.

    proxy_server = {"http":"86.51.26.13:8080"}
    proxy = urllib2.ProxyHandler(proxy_server)
    opener = urllib2.build_opener(proxy)
    urllib2.install_opener(opener)

    response = urllib2.urlopen("http://www.whatismyip.com/").read()
    print response

The error I get using the code above is:

urllib2.HTTPError: HTTP Error 403: Forbidden

The proxy server is alright(I can use it in Firefox). Moreover, I wont see any communication(Wireshark) with my computer to the destination address something that really weird(how does urllib2 determine the http error code?)

Any suggestions?

user3061385
  • 157
  • 1
  • 2
  • 9
  • Are you using a SOCKS proxy with Firefox? If so, try the approach in [this answer](http://stackoverflow.com/a/2339260/3618671). – Misha Brukman Jun 06 '14 at 15:13

1 Answers1

0

I believe, your code is almost correct, you just need to specify the protocol your script should talk to the proxy. Try:

import urllib2

proxy_server = {"http":"http://86.51.26.13:8080"}
proxy = urllib2.ProxyHandler(proxy_server)
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)

response = urllib2.urlopen("http://www.whatismyip.com").read()
print response

The first http specifies, that this proxy will handle http request, but you could route them trough https on the proxy if you wanted to.

You might be happy with this code, but I personally prefer requests, which is a library that makes http request a lot easier to read. Compare the above equivalent in requests:

import requests
proxies = {
  "http":"http://86.51.26.13:8080"
}

response = requests.get("http://www.whatismyip.com", proxies=proxies)
print response
madmuffin
  • 963
  • 10
  • 26
  • I tried to use the first code, but I didn't work. The error is the same as before. I don't want to use a third-party python module, I want it to be independently. – user3061385 Jun 06 '14 at 15:27
  • Try some other website. When I just tried to get whatismyip.com using wget on the console, I also got a 403 - seems they are sometimes blocking non-browser traffic. – madmuffin Jun 10 '14 at 19:52