python requests [Errno 104] Connection reset by peer
有个需求,数据库有个表有将近 几千条 url 记录,每条记录都是一个图片,我需要请求他们拿到每个图片存到本地。一开始我是这么写的(伪代码):
import requests
for url in urls:
try:
r = requests.get(url).content
save_image(r)
except Exception, e:
print str(e)
然而在服务器上运行时, 会发现每隔一些请求会报类似下面的错误:
HTTPConnectionPool(host='wx.qlogo.cn', port=80):
Max retries exceeded with url: /mmopen/aTVWntpJLCAr2pichIUx8XMevb3SEbktTuLkxJLHWVTwGfkprKZ7rkEYDrKRr5icyDGIvU4iasoyRrqsffbe3UUQXT5EfMEbYKg/0 (
Caused by <class 'socket.error'>: [Errno 104] Connection reset by peer)
原因,大概是因为我频繁请求,服务器关闭了部门请求连接
import requests
for url in urls:
for i in range(10):
try:
r = requests.get(url).content
except Exception, e:
if i >= 9:
do_some_log()
else:
time.sleep(0.5)
else:
time.sleep(0.1)
break
save_image(r)
代码很简陋,但可以说明大体解决方案,在每个请求间增加延时可以减少大部分请求拒绝,
但还是存在一些请求被拒绝的,所以在那部分请求被拒绝后,发起重试,
在被拒 10 次后才善罢甘休(记录到日志)。
在实际的请求中,加了 0.1s 的延迟被拒绝的情况明显少了很多,
被拒绝重试的次数最多为 3 次,最后成功地取下了全部图片。
python requests [Errno 104] Connection reset by peer的更多相关文章
- Python 频繁请求问题: [Errno 104] Connection reset by peer
Table of Contents 1. 记遇到的一个问题:[Errno 104] Connection reset by peer 记遇到的一个问题:[Errno 104] Connection r ...
- 【Azure Redis 缓存】 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connection reset by peer"
问题描述 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connecti ...
- urllib2.URLError: <urlopen error [Errno 104] Connection reset by peer>
http://www.dianping.com/shop/8010173 File "综合商场1.py", line 152, in <module> httpC ...
- celery使用rabbitmq报错[Errno 104] Connection reset by peer.
写好celery任务文件,使用celery -A app worker --loglevel=info启动时,报告如下错误: [2019-01-29 01:19:26,680: ERROR/MainP ...
- fwrite(): send of 8192 bytes failed with errno=104 Connection reset by peer
问题:fwrite(): send of 8192 bytes failed with errno=104 Connection reset by peer 问题描述 通过mysql + sphinx ...
- Lost connection to MySQL server during query ([Errno 104] Connection reset by peer)
Can't connect to MySQL server Lost connection to MySQL server during query · Issue #269 · PyMySQL/Py ...
- nginx错误分析 `104: Connection reset by peer`
故障描述 应用从虚拟机环境迁移到kubernetes环境中,有些应用不定时出现请求失败的情况,且应用没有记录任何日志,而在NGINX中记录502错误.我们查看了之前虚拟机中的访问情况,没有发现该问题. ...
- gem install 出现Errno::ECONNRESET: Connection reset by peer - SSL_connect (https://api.rubygems.org
在安装了rvm来管理多版本的ruby之后,想在不同环境下安装一些gems,结果gem install puma 之后,发现一次又一次失败. gem install 出现Errno::ECONNRESE ...
- OGG-01232 Receive TCP params error: TCP/IP error 104 (Connection reset by peer), endpoint:
源端: 2015-02-05 17:45:49 INFO OGG-01815 Virtual Memory Facilities for: COM anon alloc: mmap(MAP_ANON) ...
随机推荐
- Linux系统WEB服务之Nginx基础入门
一.Nginxi简介 Nginx是什么?它是一个开源.高性能的WEB服务器软件和代理服务器软件,由俄罗斯人Igor Sysoev 开发实现.它的功能主要分三类,第一是它作为一个WEB服务软件使用:第二 ...
- 39.Python模板结构优化-引入模板include标签、模板继承使用详解
在进行模板的构造时,不免有些模板的部分样式会相同,如果每一个模板都是重写代码的话,不仅在做的时候麻烦,而且在后期的维护上,也是相当的麻烦.所以我们可以将模板结构进行优化,优化可以通过:引入模板:模板继 ...
- matlab 中 find() 函数用法
一. 功能: 寻找非零元素的索引和值 二.相关函数语法: ind = find(X) ind = find(X, k) ind = find(X, k, 'first') ind = find(X, ...
- 9个常用的正则表达式-sunziren
正数字:/^[1-9]{1}[0-9]*$|^0{1}\.{1}[0-9]+$|^[1-9]{1}[0-9]*\.{1}[0-9]+$/ 用户名:/^[a-z0-9_-]{3,16}$/ 密码:/^[ ...
- wondiws+centos 双系统
任务:在windows下安装CentOS 1.下载CentOS镜像文件,准备一个未格式化的空间. 2.使用UltraISO将要安装的系统写入U盘. 3.用U盘启动,将系统装入一个空的分区下(未格式化) ...
- 剑指offer-面试题56_1-数组中只出现一次的两个数字-位运算
/* 题目: 求数组A中只出现一次的数字,该数组中有2个数字a.b仅出现一次,其余均出现两次 */ /* 思路: 两个相同的数字异或为0. 遍历数组,得到数组中各数字异或后的结果x,结果x=a^b. ...
- Python2与Python3比较
1.print 函数 1. print语句没有了,取而代之的是print()函数. Python 2.6与Python 2.7部分地支持这种形式的print语法. 2.Unicode 1. 在pyt ...
- ImportError: DLL load failed with error code -1073741795
Win7,python3.6,pip安装tensorflow之后报错: >>> import tensorflow Traceback (most recent call last) ...
- 获取WEB图片
public string GetJpgFile(string strFileServerPath ,string strReportDir) { string strPath = "&qu ...
- Object的对象的相关方法
Object.getPrototypeOf() Object.getPrototypeOf方法返回参数对象的原型.这是获取原型对象的标准方法. var F = function () {}; var ...