Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python urllib2请求超时_Python_Python Requests_Urllib2 - Fatal编程技术网

Python urllib2请求超时

Python urllib2请求超时,python,python-requests,urllib2,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,*

我的代码:

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)
如何添加5秒的超时时间?

添加以下内容:

import socket
socket.setdefaulttimeout(5)
(摘自)

添加以下内容:

import socket
socket.setdefaulttimeout(5)

(取自)

python没有设置默认超时吗?我刚刚尝试了
socket.getdefaulttimeout()
如果在
socket.setdefaulttimeout(value)
之前调用它,它会返回
None
,python没有设置默认超时吗?我刚刚尝试了
socket.getdefaulttimeout()
如果在socket.setdefaulttimeout(value)之前调用它,它将返回
None