Python 2.6 之前版本中 urllib2.urlopen() 的超时
urllib2 文档 表示 Python 2.6 中添加了 timeout 参数。不幸的是我的代码库一直在Python 2.5和2.4平台上运行。
有没有其他方法来模拟超时?我想做的就是允许代码在固定的时间内与远程服务器通信。
也许有任何替代的内置库? (不想安装第 3 方,例如 pycurl)
The urllib2 documentation says that timeout parameter was added in Python 2.6. Unfortunately my code base has been running on Python 2.5 and 2.4 platforms.
Is there any alternate way to simulate the timeout? All I want to do is allow the code to talk the remote server for a fixed amount of time.
Perhaps any alternative built-in library? (Don't want install 3rd party, like pycurl)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用以下命令为所有套接字操作(包括 HTTP 请求)设置全局超时:
< code>socket.setdefaulttimeout()
像这样:
在这种情况下,您的 urllib2 请求将在 30 秒后超时并抛出套接字异常。 (这是在Python 2.3中添加的)
you can set a global timeout for all socket operations (including HTTP requests) by using:
socket.setdefaulttimeout()
like this:
in this case, your urllib2 request would timeout after 30 secs and throw a socket exception. (this was added in Python 2.3)
令人相当恼火的是,您可以重写 urllib2.HTTPHandler 使用的 httplib.HTTPConnection 类。
With considerable irritation, you can override the httplib.HTTPConnection class that the urllib2.HTTPHandler uses.
我认为你最好的选择是用 2.6 维护分支的更改
该文件应位于
/usr/lib/python2.4/urllib2.py
(在 Linux 和 2.4 上)I think your best choice is to patch (or deploy an local version of) your urllib2 with the change from the 2.6 maintenance branch
The file should be in
/usr/lib/python2.4/urllib2.py
(on linux and 2.4)我使用标准库中的 httplib。它有一个极其简单的 API,但正如您可能猜到的那样,它只处理 http。 IIUC urllib 使用 httplib 来实现 http 的东西。
I use httplib from the standard library. It has a dead simple API, but only handles http as you might guess. IIUC urllib uses httplib to implement the http stuff.
您必须在两个地方设置超时。
You must set timeout in two places.
嗯,2.4 或 2.6 中处理超时的方式是相同的。如果你在 2.6 中打开 urllib2.py 文件,你会发现它需要一个额外的参数作为超时,并使用答案 1 中提到的 socket.defaulttimeout() 方法来处理它。
所以你真的不需要更新你的 urllib2.py案件。
Well, the way timeout is handled in either 2.4 or 2.6 is the same. If you open the urllib2.py file in 2.6 u would see that it takes an extra argument as timeout and handles it using the socket.defaulttimeout() method as mentioned is answer 1.
So you really need not update your urllib2.py in that case.