gpt4 book ai didi

Python 总是返回 Network is unreachable because of old ipv6 configuration

转载 作者:太空狗 更新时间:2023-10-30 02:48:58 27 4
gpt4 key购买 nike

我目前对我使用 Python 发出的任何请求都收到网络无法访问错误。无论我使用的是 urllib 库还是 requests 库。

经过更多研究后,它很可能是由错误设置的 ipv6 隧道引起的,该隧道似乎仍处于事件状态:

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

一些上下文:我正在运行 Archlinux 并今天更新了系统,尽管今天似乎没有任何与 python 相关的特殊更新。我也在 virtualenv 下运行它,但是其他 virtualenvs 和在 virtualenv 之外使用我的 Python 也有同样的问题。
我正在使用 VPN,但如果没有 VPN,我也会遇到同样的错误。我也尝试重新启动 PC,哈哈,这通常有助于解决任何问题,哈哈,但也没有帮助。
我感觉它可能与 Archlinux 相关,但我不确定。

这是我之前尝试设置 ipv6 隧道的方法:

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

也用到了这个命令:

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

在删除/etc/systemctl.conf 中的 ipv6 行后更新 3,一些 url 开始工作:

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

这是 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>

我可以从网络浏览器正常访问 google.com,我很确定网络可以访问。

最佳答案

您确定您没有使用任何 HTTP 代理服务器访问互联网吗?

尝试将浏览器中的网络设置更改为无代理,并检查它是否仍在连接到互联网。

如果您正在使用代理(假设代理地址为 http://yourproxy.com),请尝试执行此操作以检查是否可以解决问题。

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

关于Python 总是返回 Network is unreachable because of old ipv6 configuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10450776/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com