gevent和requests同时使用报requests.exceptions.ReadTimeout:HTTPSConnectionPool(host='www.baidu.com', port=4

最近项目需要用协程来处理网站并发访问,使用了Gevent这个库,访问网站用requests,然而访问https的url时报错:

>>> from gevent import monkey; monkey.patch_socket()
>>> import gevent
>>> url='https://www.baidu.com'
>>> requests.get(url)
requests.exceptions.ReadTimeout:HTTPSConnectionPool(host='www.baidu.com', port=443): Read timed out. (read timeout=None)

一开始还以为是网络连接的问题,然而使用wget并没有问题。设定requests的timeout参数同样没有作用。改用urllib2库,报的是下面这个异常:

urllib2.URLError: error [Errno 2] _ssl.c:504: The operation did not complete (read)>

通过下面这个网站:
http://stackoverflow.com/questions/20580252/python-requests-module-throws-exception-with-gevent

得到了解决方法:

>>> from gevent import monkey; monkey.patch_ssl()
>>> requests.get(url)
200]>

这次的问题是因为对Gevent库不熟悉,同时只想着粘贴代码完成功能。结果GG了。

你可能感兴趣的:(Python)