python - 尽管超时,urllib2.urlopen 将永远挂起

标签 python http timeout settimeout urllib2

希望这是一个非常简单的问题,但它让我发疯。 我在 ubuntu 12.10 服务器的开箱即用安装中使用 Python 2.7.3。我一直在关注这个问题,直到我看到这个片段:

import urllib2
x=urllib2.urlopen("http://casacinema.eu/movie-film-Matrix+trilogy+123+streaming-6165.html", timeout=5)

它只是永远挂起,永远不会超时。 我显然做错了什么。有人可以帮忙吗? 真的非常感谢!

马特奥

最佳答案

您似乎遇到了代理问题。这是关于如何解决它的一个很好的解释: Trying to access the Internet using urllib2 in Python .

我已经在我的 ubuntu 上用 python 2.7.3 执行了你的代码,没有发现任何错误。

另外,考虑使用 requests :

import requests

response = requests.get("http://casacinema.eu/movie-film-Matrix+trilogy+123+streaming-6165.html", timeout=5)
print response.status_code

另见:

关于python - 尽管超时,urllib2.urlopen 将永远挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16772795/

相关文章:

internet-explorer - IE 接受标题更改,为什么?

c# - 处理来自 RestSharp 的错误响应的正确策略是什么?

Python:运行一个进程/线程,即使在主进程完成后仍继续运行

python - "\"和 "|"在 Python 中如何使用(不在字符串中)

python - 投资组合优化的蒙特卡罗方法

php - HTTP header 阻止浏览器加载页面?

timeout - 如何优雅地终止无响应的 tcl 脚本?

windows - Golang、net.TCPConn、SetReadTimeout?

oracle - PL/SQL Developer (allroundautomations.com) 在编译包主体时挂起

python - IPython中带有多个参数的并行映射函数