2016-08-09 66 views
0

我的代碼:Python的urllib2的請求超時

link = "http://www.google.com" 
request_headers = {"Accept-Language": "en-US,en;q=0.5", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Referer": "http://google.com", "Connection": "keep-alive"} 
request = urllib2.Request(link, headers=request_headers) 

如何添加五秒超時?

回答

2

添加以下內容:

import socket 
socket.setdefaulttimeout(5) 

(從urllib2 timeout拍攝)

+0

是否存在被蟒蛇沒有設定默認的超時? –

+1

我只是試過'socket.getdefaulttimeout()'它返回'None',如果你在'socket.setdefaulttimeout(value)之前調用' – biscuit314