gpt4 book ai didi

互联网连接中断的 Python 的 urllib.request.urlopen

转载 作者:行者123 更新时间:2023-11-28 17:40:49 24 4
gpt4 key购买 nike

我在 python 的 urllib 和中断的互联网连接方面遇到了一些问题:在没有事件互联网连接的情况下第一次调用它时,我永远无法从 urllib.request.urlopen 获取信息。以下工作正常:

 > python
>>> import urllib.request
>>> urllib.request.urlopen("http://www.google.com")
<http.client.HTTPResponse object at 0x7f6f54681438>

#Now disable internet connection:
> sudo ip link set enp4s0 down

>>> urllib.request.urlopen("http://www.google.com")
Traceback (most recent call last):
File "/usr/lib/python3.4/urllib/request.py", line 1189, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/usr/lib/python3.4/http/client.py", line 1090, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.4/http/client.py", line 1128, in _send_request
self.endheaders(body)
File "/usr/lib/python3.4/http/client.py", line 1086, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.4/http/client.py", line 924, in _send_output
self.send(msg)
File "/usr/lib/python3.4/http/client.py", line 859, in send
self.connect()
File "/usr/lib/python3.4/http/client.py", line 836, in connect
self.timeout, self.source_address)
File "/usr/lib/python3.4/socket.py", line 491, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.4/socket.py", line 530, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/urllib/request.py", line 153, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.4/urllib/request.py", line 455, in open
response = self._open(req, data)
File "/usr/lib/python3.4/urllib/request.py", line 473, in _open
'_open', req)
File "/usr/lib/python3.4/urllib/request.py", line 433, in _call_chain
result = func(*args)
File "/usr/lib/python3.4/urllib/request.py", line 1215, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "/usr/lib/python3.4/urllib/request.py", line 1192, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -2] Name or service not known>

#Reenable internet connection:
> sudo ip link set enp4s0 up #and wait a bit

>>> urllib.request.urlopen("http://www.google.com")
<http.client.HTTPResponse object at 0x7f6f5468c898>

到目前为止一切顺利。现在完全相同,但没有第一次调用 urlopen:

 > python
>>> import urllib.request
# do not call urlopen before internet is down...


#Now disable internet connection:
> sudo ip link set enp4s0 down

>>> urllib.request.urlopen("http://www.google.com")
[exactly the same error message as above]

#Reenable internet connection:
> sudo ip link set enp4s0 up #and wait a bit

#Ensure internet connection is up
> ip link show enp4s0 up
2: enp4s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP [...]


>>> urllib.request.urlopen("http://www.google.com")
[exactly the same error message as above]
#What's the problem? The internet connection IS up

#However:
> host www.google.com
www.google.com has address 173.194.69.104
[...]
>>> urllib.request.urlopen("http://173.194.69.104")
<http.client.HTTPResponse object at 0x7f3116a72e48>

所以我想它必须对 DNS(-缓存)做些什么?

最后是关于我的系统的一些信息:

 > python --version
Python 3.4.1
> uname -a
Linux charon 3.15.3-1-ARCH #1 SMP PREEMPT Tue Jul 1 07:32:45 CEST 2014 x86_64 GNU/Linux

抱歉奇怪的格式。我混淆了“普通”(以“>”为前缀)和 python(以“>>>”为前缀)shell 命令以使确切的命令序列清晰(这显然发生在不同的终端)。

最佳答案

您正在运行 well-known glibc problem .人们可以争论这是对 glibc 的误用还是 glibc 在这里做错了什么。 res_init 不是 POSIX 的一部分,而是源自 BSD 的接口(interface),因此很难以平台无关的方式正确执行。

对于这个问题似乎没有针对 python 的错误报告,所以你可能想要 file one .

作为一种变通方法,您可以使用 ctypes 自己调用 res_init,但我不知道该怎么做.

关于互联网连接中断的 Python 的 urllib.request.urlopen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24588010/

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