python下载时报错 Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time

时间:2022-12-31 00:06:43
def downloadXml(isExists,filedir,filename):
if not isExists:
os.mkdir(filedir)
local = os.path.join(filedir,filename)
urllib2.urlopen(url,local)

报错:

Traceback (most recent call last):
File "C:\Users\william\Desktop\nova xml\New folder\download_xml.py", line 95, in <module>
downloadXml(isExists,filedir,filename)
File "C:\Users\william\Desktop\nova xml\New folder\download_xml.py", line 80, in downloadXml
urllib.urlretrieve(url,local)
File "E:\Python27\lib\urllib.py", line 98, in urlretrieve
return opener.retrieve(url, filename, reporthook, data)
File "E:\Python27\lib\urllib.py", line 245, in retrieve
fp = self.open(url, data)
File "E:\Python27\lib\urllib.py", line 213, in open
return getattr(self, name)(url)
File "E:\Python27\lib\urllib.py", line 350, in open_http
h.endheaders(data)
File "E:\Python27\lib\httplib.py", line 1053, in endheaders
self._send_output(message_body)
File "E:\Python27\lib\httplib.py", line 897, in _send_output
self.send(msg)
File "E:\Python27\lib\httplib.py", line 859, in send
self.connect()
File "E:\Python27\lib\httplib.py", line 836, in connect
self.timeout, self.source_address)
File "E:\Python27\lib\socket.py", line 575, in create_connection
raise err
IOError: [Errno socket error] [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
>>>

google查找答案,搜索:urlretrieve Errno 10060

在 https://segmentfault.com/q/1010000004386726中提到是:频繁的访问某个网站会被认为是DOS攻击,通常做了Rate-limit的网站都会停止响应一段时间,你可以Catch这个Exception,sleep一段时间然后重试,也可以根据重试的次数做exponential backup off。

想了一个简单的办法,就是每次下载之间加个延时,将代码修改如下:

def downloadXml(isExists,filedir,filename):
if not isExists:
os.mkdir(filedir)
local = os.path.join(filedir,filename)
time.sleep(1)
urllib.urlretrieve(url,local)

执行。 本来是在第80条左右的数据就开始time out,但现在一直执行到2300多条数据。可惜,最后又time out。

这里,若延长延时,将1s改为5s等,虽然可能不会报错,但我想,这样,太费时间了。因为不报错时,也要延时5s,不如等报错时再延时重试。

于是,

def downloadXml(isExists,filedir,filename):
if not isExists:
os.makedirs(filedir)
local = os.path.join(filedir,filename)
try:
urllib.urlretrieve(url,local)
except Exception as e:
time.sleep(5)
urllib.urlretrieve(url,local)

这样的话,发现会卡在某条数据,不向后执行。所以只好改为在某条数据上,最多重试10次。

def downloadXml(flag_exists,file_dir,file_name,xml_url):
if not flag_exists:
os.makedirs(file_dir)
local = os.path.join(file_dir,file_name)
try:
urllib.urlretrieve(xml_url,local)
except Exception as e:
print e
cur_try = 0
total_try = 10
if cur_try < total_try:
cur_try +=1
time.sleep(15)
return downloadXml(flag_exists,file_dir,file_name,xml_url)
else:
raise Exception(e)

这样执行后,果然不再报错,顺利执行完了。但一想,有个问题,使用哪个URL进行下载失败,没有记录下来。所以又添加了将失败的url写入本地文本的功能。后面可以查看,并手动执行。

def downloadXml(flag_exists,file_dir,file_name,xml_url):
if not flag_exists:
os.makedirs(file_dir)
local = os.path.join(file_dir,file_name)
try:
urllib.urlretrieve(xml_url,local)
except Exception as e:
print 'the first error: ',e
cur_try = 0
total_try = 10
if cur_try < total_try:
cur_try +=1
time.sleep(15)
return downloadXml(flag_exists,file_dir,file_name,xml_url)
else:
print 'the last error: '
with open(test_dir + 'error_url.txt','a') as f:
f.write(xml_url)
raise Exception(e)

遗憾的是,这次竟再没有失败的url了,可能是网站这时流量不大。

python下载时报错 Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time的更多相关文章

  1. BUG&colon;upstream timed out &lpar;10060&colon; A connection attempt failed because the connected party did not properly respond after a period of time&comma; or established connection failed because connected

    更换Apache扑向Nginx,刚搭建完WNMP,nginx能访问php页面 但是访问现有开发项目报错 [error] 4112#3724: *9 upstream timed out (10060: ...

  2. upstream timed out &lpar;10060&colon; A connection attempt failed because the connected party did not properly respond

    openresty 错误日志报错内容: // :: [error] #: * upstream timed : A connection attempt failed because the conn ...

  3. VScode 1&period;13 gocode提示dial tcp 216&period;239&period;37&period;1&colon;443&colon; connectex&colon; A connection attempt failed because the connected&period;&period;

    在将VScode升级至 1.13后让升级gocode,在升级时报出如下错误 D:\go_work\src>go get -u -v github.com/mdempsky/gocode gith ...

  4. vs code解决golang开发环境问题 dial tcp 216&period;239&period;37&period;1&colon;443&colon; connectex&colon; A connection attempt failed

    安装插件是出现 如下错误提示, https fetch failed: Get https://golang.org/x/tools/cmd/gorename?go-get=1: dial tcp 2 ...

  5. windows下pip安装python模块时报错

    windows下pip安装python模块时报错总结  装载于:https://www.cnblogs.com/maxaimee/p/6515165.html 前言: 这几天把python版本升级后, ...

  6. windows下pip安装python模块时报错【转】

    windows下pip安装python模块时报错总结 请给作者点赞--> 原文链接 1 权限问题 C:\Users\ljf>pip install xlwt Exception: Trac ...

  7. FetchType&period;LAZY 时属性加上&commat;JsonIgnore,避免返回时报错:Could not write JSON&colon; failed to lazily initialize a collection of role

    [示例] @OneToMany(fetch=FetchType.LAZY) @JsonIgnore @Fetch(FetchMode.SELECT) @Cascade(value={CascadeTy ...

  8. wget http&colon;&sol;&sol;pypi&period;python&period;org&sol;packages&sol;source&sol;s&sol;setuptools&sol;setuptools-2&period;0&period;tar&period;gz 下载时报错 ssl is required 解决办法

    方法一:使用浏览器下载.在浏览器中输入 http://pypi.python.org/packages/source/s/setuptools/setuptools-2.0.tar.gz 方法二:将h ...

  9. python 启动时报错无法正常启动(0xc000007b)请单击&OpenCurlyDoubleQuote;确定”关闭应用程序的解决办法

    这是一个自己非常傻逼的问题,但是还是想记录下来 晚上安装python,不管是命令提示符中运行还是python直接打开,都提示报错 各种百度,各种查找排除以后,皆不能解决错误 最后发现:特么64位系统下 ...

随机推荐

  1. 分布式大数据高并发的web开发框架

    一.引言 通常我们认为静态网页html的网站速度是最快的,但是自从有了动态网页之后,很多交互数据都从数据库查询而来,数据也是经常变化的,除了一些新闻资讯类的网站,使用html静态化来提高访问速度是不太 ...

  2. Python-01-基础

    一.安装Python 官方下载地址:https://www.python.org/downloads/ Windows可直接下载安装,安装时勾选自动配置环境变量即可. Linux/OS X默认装有Py ...

  3. Arduino101学习笔记(十)&mdash&semi;&mdash&semi; 串口通信

    //打开串口 Serial.begin(); //获取串口上可读取的数据的字节数.该数据是指已经到达并存储在接收缓存(共有64字节)中 Serial.available(); //读串口数据,串口上第 ...

  4. iOS开发Swift篇—简单介绍

    iOS开发Swift篇—简单介绍 一.简介 Swift是苹果于2014年WWDC(苹果开发者大会)发布的全新编程语言 Swift在天朝译为“雨燕”,是它的LOGO 是一只燕子,跟Objective-C ...

  5. Uva----------&lpar;11078&rpar;Open Credit System

    Open Credit System Input:Standard Input Output: Standard Output In an open credit system, the studen ...

  6. Leetcode&num;150&Tab;Evaluate Reverse Polish Notation

    原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...

  7. DeepLearning常用库简要介绍与对比

    网上近日流传一张DL相关库在Github上的受关注度对比(数据应该是2016/03/15左右统计的): 其中tensorflow,caffe,keras和Theano排名比较靠前. 今日组会报告上tj ...

  8. dojo中获取表格中某一行的某个值

    dojo中经常出现对表格中的某行进行操作,如单击某行修改.删除等.那怎样获取某行的唯一标示呢? 如查询表格中的某列有个userId,并且这个是唯一的,那么可以通过它来访问这一列 具体操作代码如下: v ...

  9. Android studio 使用startService报错:IllegalStateException

    Android 8.0启动service报错:java.lang.RuntimeException和java.lang.IllegalStateException 错误信息: java.lang.Ru ...

  10. ios 解决Wkwebview闪烁问题

    // 网页闪烁问题    if ([self.webView.realWebView isKindOfClass:[WKWebView class]]) {         ((WKWebView * ...