Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

urllib2.URLError: <urlopen error Tunnel connection failed: 403 Tunnel or SSL Forbidden>

Tags:

python

urllib2

Basically ,I'm trying to use python's urllib2. I want to connect and fetch the data from a site. The problem is that I get the error

urllib2.URLError: <urlopen error Tunnel connection failed: 403 Tunnel or SSL Forbidden>

After repeating my experiments with this library , I found that the code I had written worked well with https:// sites but not with http:// sites. I read a few earlier questions on stack overflow suggesting to add the header User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (to spoof the header).

I did that but still it failed.

After that I read this urllib2.HTTPError: HTTP Error 403: Forbidden

I tried that as well but that didn't work.

Here's my code

import urllib2
url = "http://the_site_i_want_to_connect"
hdr = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7','Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
req = urllib2.Request(url , headers=hdr)
p = urllib2.urlopen(req).read()
print p

PS: As I said , this works fine with https

Please help! Thanks in advance!

like image 761
Punit Dhoot Avatar asked Apr 20 '15 09:04

Punit Dhoot


1 Answers

This error looks like issue with your proxy settings please refer this blog

like image 52
Krishna Avatar answered Oct 24 '22 00:10

Krishna