Python always returning Network is unreachable because of old ipv6 configuration

落花浮王杯 提交于 2019-12-03 08:29:00

Are you sure you are not using any http proxy server to get to internet?

Try changing the network settings in your browser to no-proxy and check if it is still connecting to internet.

And if you are using proxy ( assume proxy address as http://yourproxy.com) then try doing this to check if this solves the issue.

import urllib2
proxy = urllib2.ProxyHandler({'http': 'yourproxy.com'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
urllib2.urlopen('http://www.google.com')

The big thing was that I edited my /etc/hosts file by putting back the backup of the file everything started working again. I did this to bypass the great firewall to manually set the ipv6 addresses of facebook etc, so it was still using those ipv6 addresses...

Lesson learned: Don't work too much and dont do things in a hurry. Always try to understand what you're doing and write down what you did exactly. So you have a way to fall back.

Removing the following line seemed to help a little bit in /etc/systemctl.conf:

net.ipv6.conf.all.forwarding = 1

It didn't help in the end, still getting the error Network is unreachable for google.com although its accesible in my browser:

>>> urllib2.urlopen('http://baidu.com')
<addinfourl at 27000560 whose fp = <socket._fileobject object at 0x7f4d1fed5e50>>
>>> urllib2.urlopen('http://www.google.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 418, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1207, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1177, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 101] Network is unreachable>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!