Python always returning Network is unreachable because of old ipv6 configuration

江枫思渺然 提交于 2019-12-04 13:40:26

问题


I currently get a Network is unreachable error to any request I'm making with python. No matter if im using the urllib library or the requests library.

After some more research its likely that its being caused by an incorrect setup ipv6 tunnel, which seems to be still active:

$ ip -6 addr show  
$ ip -6 route
default dev wlan0  metric 1

Some context: I'm running Archlinux and updated the system today, although there didnt seem to be any special updates today related to python. I'm also running this under a virtualenv, but other virtualenvs and using my Python outside the virtualenv also have the same problem.
I'm using a VPN, but also without the VPN I get the same error. I also tried restarting the PC, haha that normally helps with any problem lol but also didnt help.
I got a feeling it may be Archlinux related but I'm not sure.

This is what I tried before to setup a ipv6 tunnel:

sudo modprobe ipv6
sudo ip tunnel del sit1
sudo ip tunnel add sit1 mode sit remote 59.66.4.50 local $ipv4
sudo ifconfig sit1 down
sudo ifconfig sit1 up
sudo ifconfig sit1 add 2001:da8:200:900e:0:5efe:$ipv4/64
sudo ip route add ::/0 via 2001:da8:200:900e::1 metric 1 

Also used this command:

ip -6 addr add 2001:0db8:0:f101::1/64 dev eth0 

Update 3 after removing line of ipv6 in my /etc/systemctl.conf, some urls started working:

>>> 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>
>>> urllib2.urlopen('http://baidu.com')
<addinfourl at 27000560 whose fp = <socket._fileobject object at 0x7f4d1fed5e50>>

This is the error log from ipython.

In [1]: import urllib2

In [2]: urllib2.urlopen('http://google.com')
---------------------------------------------------------------------------
URLError                                  Traceback (most recent call last)

/home/samos/<ipython console> in <module>()

/usr/lib/python2.7/urllib2.py in urlopen(url, data, timeout)
    124     if _opener is None:
    125         _opener = build_opener()
--> 126     return _opener.open(url, data, timeout)
    127 
    128 def install_opener(opener):

/usr/lib/python2.7/urllib2.py in open(self, fullurl, data, timeout)
    398             req = meth(req)
    399 
--> 400         response = self._open(req, data)
    401 
    402         # post-process response


/usr/lib/python2.7/urllib2.py in _open(self, req, data)
    416         protocol = req.get_type()
    417         result = self._call_chain(self.handle_open, protocol, protocol +
--> 418                                   '_open', req)
    419         if result:
    420             return result

/usr/lib/python2.7/urllib2.py in _call_chain(self, chain, kind, meth_name, *args)
    376             func = getattr(handler, meth_name)
    377 
--> 378             result = func(*args)
    379             if result is not None:
    380                 return result

/usr/lib/python2.7/urllib2.py in http_open(self, req)
   1205 
   1206     def http_open(self, req):
-> 1207         return self.do_open(httplib.HTTPConnection, req)
   1208 
   1209     http_request = AbstractHTTPHandler.do_request_

/usr/lib/python2.7/urllib2.py in do_open(self, http_class, req)
   1175         except socket.error, err: # XXX what error?
   1176             h.close()
-> 1177             raise URLError(err)
   1178         else:
   1179             try:

URLError: <urlopen error [Errno 101] Network is unreachable>

I can access google.com normally from a web browser and im pretty sure the network is reachable.


回答1:


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')



回答2:


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>


来源:https://stackoverflow.com/questions/10450776/python-always-returning-network-is-unreachable-because-of-old-ipv6-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!