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: <urlopen 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)
<Response [200]>

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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值