python设置socket的超时时间(可能使用locust压测千级并发的时候要用到,先记录在此)

 

在使用urllib或者urllib2时,有可能会等半天资源都下载不下来,可以通过设置socket的超时时间,来控制下载内容时的等待时间。

如下python代码

import socket  

timeout = 20    
socket.setdefaulttimeout(timeout)

这样默认20秒之后就会超时。

参考 http://outofmemory.cn/code-snippet/1862/python-setting-socket-chaoshi-time

原文地址:https://www.cnblogs.com/kaibindirver/p/11805565.html