Python urllib2请求超时

时间:2016-08-09 18:34:35

标签: python python-requests 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)

如何添加五秒的超时?

1 个答案:

答案 0 :(得分:2)

添加以下内容:

import socket
socket.setdefaulttimeout(5)

(摘自urllib2 timeout