如何强制 urllib2 超时? [英] How can I force urllib2 to time out?

查看:38
本文介绍了如何强制 urllib2 超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在通过 urllib2 抓取数据时测试我的应用程序对超时的处理,并且我想有某种方法来强制请求超时.

I want to to test my application's handling of timeouts when grabbing data via urllib2, and I want to have some way to force the request to timeout.

找不到非常慢的互联网连接,我可以使用什么方法?

Short of finding a very very slow internet connection, what method can I use?

我似乎记得有一个有趣的应用程序/套件可以模拟这些事情.也许有人知道链接?

I seem to remember an interesting application/suite for simulating these sorts of things. Maybe someone knows the link?

推荐答案

我通常使用 netcat 监听本地机器的 80 端口:

I usually use netcat to listen on port 80 of my local machine:

nc -l 80

然后我在我的应用程序中使用 http://localhost/ 作为请求 URL.Netcat 将在 http 端口应答但永远不会给出响应,因此只要您在 urllib2.urlopen() 调用或通过调用 urllib2.urlopen()代码>socket.setdefaulttimeout().

Then I use http://localhost/ as the request URL in my application. Netcat will answer at the http port but won't ever give a response, so the request is guaranteed to time out provided that you have specified a timeout in your urllib2.urlopen() call or by calling socket.setdefaulttimeout().

这篇关于如何强制 urllib2 超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆