Python urllib2 giving “network unreachable error”

2019-05-23 12:27发布

I am trying to fetch some urls using urllib2 library.

a = urllib2.urlopen("http://www.google.com")
ret = a.read()

Code above is working fine, and giving expected result. But when I make the url https, it gives "network unreachable" error

a = urllib2.urlopen("https://www.google.com")
urllib2.URLError: <urlopen error [Errno 101] Network is unreachable>

Is there any problem with ssl? My python version is Python2.6.5. I am also behind an academic proxy server. I have the settings in bash file. Anyway, since http is opening proxy shouldn't be the problem here.

2条回答
冷血范
2楼-- · 2019-05-23 13:13

The http url didn't give error because http_proxy variable was set already. By setting https_proxy the above error disappears.

export http_proxy = "http://{proxy-address}"

Set samething for https_proxy

export https_proxy = "http://{proxy-address}"
查看更多
聊天终结者
3楼-- · 2019-05-23 13:20

Normally the issue in cases like this is the proxy you are behind having an out of date or untrusted SSL certificate. urllib is fussier than most browsers when it comes to SSL and this is why you might be getting this error.

查看更多
登录 后发表回答