简体   繁体   English

针对套接字错误Errno 10060的Python try / except语句

[英]Python try/except statement for socket error Errno 10060

I have a Python 2.7 script that consists of opening/scraping multiple URLs from a database and extracting some information out of the webpages. 我有一个Python 2.7脚本,其中包括从数据库中打开/抓取多个URL并从网页中提取一些信息。 The code can take up to 8 hours to run and I am dealing with multiple websites. 该代码最多可能需要8个小时才能运行,而我正在处理多个网站。

Every now and then (1-2 hours), I randomly get the error message IOError: [Errno socket error] [Errno 10060] while trying to open a URL 偶尔(1-2小时),我尝试打开URL时随机收到错误消息IOError: [Errno socket error] [Errno 10060]

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

I tried to avoid the error by adding a 2 sec pause with time.sleep(2) between the URL opening operations but I still get the error. 我尝试通过在URL打开操作之间添加time.sleep(2)的2秒暂停来避免该错误,但仍然出现错误。 The error seems to be independent of the website from which I am trying to open the URL. 该错误似乎与我要打开URL的网站无关。

I was looking at a way to prevent my script from crashing using a try/except statement. 我正在寻找一种使用try / except语句防止脚本崩溃的方法。
In the event of a socket error, the script would pause for something like 20 sec and then, retry to open the URL. 如果发生套接字错误,脚本将暂停约20秒,然后重试打开URL。 If the URL opens correctly, move on with the script. 如果URL正确打开,请继续执行脚本。 I'm using urlopen() to open the URLs. 我正在使用urlopen()打开URL。

As your code raises an IOError , run this code, but substitute your line of error for the raise . 当您的代码引发IOError ,请运行此代码,但将您的错误行替换为raise

try:
    raise IOError
except IOError:
    time.sleep(20)
    pass
else:
    break

Since this is so rare and only happens every hour or so, you can probably blame your internet connection. 由于这种情况非常罕见,而且仅每隔一个小时左右发生一次,因此您可能会怪罪您的互联网连接。 The code you are looking for is: 您正在寻找的代码是:

import time
for url in urls:
    while True:
        try:
            response = urllib2.urlopen(url)
            #Do stuff
        except :
            time.sleep(20)
        else :
            #stops the inner loop if there is no error
            break

Python:网址错误: <urlopen error [errno 10060]< div><div id="text_translate"><p> <strong>操作系统:Windows 7;</strong> <strong>Python 2.7.3 使用 Python GUI Shell</strong></p><p> 我正在尝试通过 Python 阅读一个网站,并且有几个作者使用urllib和urllib2库。 为了将站点存储在变量中,我看到了一种类似的方法:</p><pre> import urllib import urllib2 g = "http://www.google.com/" read = urllib2.urlopen(g)</pre><p> 最后一行在 120 多秒后生成错误:</p><pre> > Traceback (most recent call last): File "<pyshell#27>", line 1, in > <module> > r = urllib2.urlopen(o) File "C:\Python27\lib\urllib2.py", line 126, in urlopen > return _opener.open(url, data, timeout) File "C:\Python27\lib\urllib2.py", line 400, in open > response = self._open(req, data) File "C:\Python27\lib\urllib2.py", line 418, in _open > '_open', req) File "C:\Python27\lib\urllib2.py", line 378, in _call_chain > result = func(*args) File "C:\Python27\lib\urllib2.py", line 1207, in http_open > return self.do_open(httplib.HTTPConnection, req) File "C:\Python27\lib\urllib2.py", line 1177, in do_open > raise URLError(err) URLError: <urlopen 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></pre><p> 我尝试绕过g变量并尝试urlopen("http://www.google.com/")也没有成功(它在相同的时间长度后生成相同的错误)。</p></div></urlopen> - Python: URLError: <urlopen error [Errno 10060]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python套接字Errno 10060 - python socket Errno 10060 Python urlopen IOError:[Errno socket错误] [Errno 10060] - Python urlopen IOError: [Errno socket error] [Errno 10060] socket.error:[Errno 10060]使用Python发送电子邮件时 - socket.error: [Errno 10060] while sending an email with Python python paramiko模块socket.error,错误10060 - python paramiko module socket.error, errno 10060 python urlopen 错误 [Errno 10060] - python urlopen error [Errno 10060] Python中的套接字编程引发错误socket.error:&lt;[Errno 10060]连接尝试失败 - Socket Programming in Python raising error socket.error:< [Errno 10060] A connection attempt failed 尝试除了语句错误python - try except statement error python 插座Errno 10060 - socket Errno 10060 使用try处理套接字错误,但[Errno 104]会被对等方重置 - Handling socket error with try, except [Errno 104] Connection reset by peer Python:网址错误: <urlopen error [errno 10060]< div><div id="text_translate"><p> <strong>操作系统:Windows 7;</strong> <strong>Python 2.7.3 使用 Python GUI Shell</strong></p><p> 我正在尝试通过 Python 阅读一个网站,并且有几个作者使用urllib和urllib2库。 为了将站点存储在变量中,我看到了一种类似的方法:</p><pre> import urllib import urllib2 g = "http://www.google.com/" read = urllib2.urlopen(g)</pre><p> 最后一行在 120 多秒后生成错误:</p><pre> > Traceback (most recent call last): File "<pyshell#27>", line 1, in > <module> > r = urllib2.urlopen(o) File "C:\Python27\lib\urllib2.py", line 126, in urlopen > return _opener.open(url, data, timeout) File "C:\Python27\lib\urllib2.py", line 400, in open > response = self._open(req, data) File "C:\Python27\lib\urllib2.py", line 418, in _open > '_open', req) File "C:\Python27\lib\urllib2.py", line 378, in _call_chain > result = func(*args) File "C:\Python27\lib\urllib2.py", line 1207, in http_open > return self.do_open(httplib.HTTPConnection, req) File "C:\Python27\lib\urllib2.py", line 1177, in do_open > raise URLError(err) URLError: <urlopen 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></pre><p> 我尝试绕过g变量并尝试urlopen("http://www.google.com/")也没有成功(它在相同的时间长度后生成相同的错误)。</p></div></urlopen> - Python: URLError: <urlopen error [Errno 10060]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM