为什么我不能让Python的urlopen()方法在Windows上工作?

时间:2022-03-16 21:01:17

Why isn't this simple Python code working?

为什么这个简单的Python代码不能工作?

import urllibfile = urllib.urlopen('http://www.google.com')print file.read()

This is the error that I get:

这是我得到的误差:

Traceback (most recent call last):  File "C:\workspace\GarchUpdate\src\Practice.py", line 26, in <module>    file = urllib.urlopen('http://www.google.com')  File "C:\Python26\lib\urllib.py", line 87, in urlopen    return opener.open(url)  File "C:\Python26\lib\urllib.py", line 206, in open    return getattr(self, name)(url)  File "C:\Python26\lib\urllib.py", line 345, in open_http    h.endheaders()  File "C:\Python26\lib\httplib.py", line 892, in endheaders    self._send_output()  File "C:\Python26\lib\httplib.py", line 764, in _send_output    self.send(msg)  File "C:\Python26\lib\httplib.py", line 723, in send    self.connect()  File "C:\Python26\lib\httplib.py", line 704, in connect    self.timeout)  File "C:\Python26\lib\socket.py", line 514, in create_connection    raise error, msgIOError: [Errno socket error] [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

I've tried it with several different pages but I can never get the urlopen method to execute correctly.

我已经尝试了几个不同的页面,但是我永远无法正确地执行urlopen方法。

3 个解决方案

#1


4  

Your code is not the problem here.

您的代码在这里不是问题所在。

Do you have any Proxy settings in your IE?

你的IE中有代理设置吗?

This says the python documentation for urllib.urlopen:
"""
In a Windows environment, if no proxy environment variables are set,
proxy settings are obtained from the registry's Internet Settings
section.
"""

这是urllib的python文档。在Windows环境中,如果没有设置代理环境变量,则从注册表的Internet设置部分获得代理设置。”“”

#2


3  

Try using urllib2 if it is feasible to change some lines of code. Set the timeout argument in seconds

如果可以更改一些代码行,请尝试使用urllib2。设置超时参数以秒为单位。

For example:

例如:

urllib2.urlopen(http://www.abc.com/api, timeout=20)

Here the connection persists for a longer duration. So if for example you are reading an XML file that is too large it avoids incomplete reading.

在这里,连接将持续更长的时间。例如,如果您正在读取一个太大的XML文件,它会避免不完整的读取。

The above code will never work if the Net connection is slow or it breaks suddenly.

如果网络连接缓慢或突然中断,上述代码将永远无法工作。

#3


1  

If you have wireshark, check what's being sent out and if there is anything coming back at all. It will help you debug the problem if you can see the GET request being sent.

如果你有wireshark,检查一下被发送出去的东西,如果有什么东西回来。如果您能够看到发送的GET请求,它将帮助您调试问题。

Also i remember having similar problem like this once, what i did was flush my dns cache

我还记得有一次遇到过类似的问题,我所做的就是刷新我的dns缓存

(ipconfig /flushdns) and restarted. It fixed my problem. It doesn't hurt to try i guess.

(ipconfig / flushdns)并重新启动。它固定我的问题。我想试试也无妨。

#1


4  

Your code is not the problem here.

您的代码在这里不是问题所在。

Do you have any Proxy settings in your IE?

你的IE中有代理设置吗?

This says the python documentation for urllib.urlopen:
"""
In a Windows environment, if no proxy environment variables are set,
proxy settings are obtained from the registry's Internet Settings
section.
"""

这是urllib的python文档。在Windows环境中,如果没有设置代理环境变量,则从注册表的Internet设置部分获得代理设置。”“”

#2


3  

Try using urllib2 if it is feasible to change some lines of code. Set the timeout argument in seconds

如果可以更改一些代码行,请尝试使用urllib2。设置超时参数以秒为单位。

For example:

例如:

urllib2.urlopen(http://www.abc.com/api, timeout=20)

Here the connection persists for a longer duration. So if for example you are reading an XML file that is too large it avoids incomplete reading.

在这里,连接将持续更长的时间。例如,如果您正在读取一个太大的XML文件,它会避免不完整的读取。

The above code will never work if the Net connection is slow or it breaks suddenly.

如果网络连接缓慢或突然中断,上述代码将永远无法工作。

#3


1  

If you have wireshark, check what's being sent out and if there is anything coming back at all. It will help you debug the problem if you can see the GET request being sent.

如果你有wireshark,检查一下被发送出去的东西,如果有什么东西回来。如果您能够看到发送的GET请求,它将帮助您调试问题。

Also i remember having similar problem like this once, what i did was flush my dns cache

我还记得有一次遇到过类似的问题,我所做的就是刷新我的dns缓存

(ipconfig /flushdns) and restarted. It fixed my problem. It doesn't hurt to try i guess.

(ipconfig / flushdns)并重新启动。它固定我的问题。我想试试也无妨。