Python 3.5.2: socket.timeout 异常导致类型错误
Python 3.5.2: socket.timeout exception causes typeerror
我有点 Python 新手,这是我第一次 post Whosebug 所以请多多包涵。 :)
在 posting 之前,我搜索了 google 和 Whosebug,但似乎找不到与我的问题类似的内容。
我有一个脚本可以轮询网站并检索内容。
它可以正常工作数小时,但是如果它遇到套接字超时,即使我有一个例外,脚本也会抛出类型错误。
我确定我遗漏了一些明显的东西,但我无法确定。
代码:
timingout = 10
def get_url(url):
try:
sock = urllib.request.urlopen(url, timeout=timingout)
orig_html = sock.read()
html = orig_html.decode("utf-8", errors="ignore").encode('cp1252', errors='ignore')
sock.close()
except KeyboardInterrupt:
# Kill program if Control-C is pressed
sys.exit(0)
except urllib.error.URLError as e:
print("***Error= Page ", e.reason)
return
except timingout:
print("socket timed out - URL: %s", url)
else:
# See if site is Down or errors eg: 404
if html == None:
print ("page contains no content!?!")
return ''
# See if site is complaining
elif html == site_overload:
if _verbose:
print('ERROR: Too many requests - SLEEPING 600 secs')
time.sleep(600)
return ''
# If not, we are good
elif html:
return html
错误:
return self._sock.recv_into(b)
**socket.timeout: timed out**
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 201, in <module>
main()
File "test.py", line 140, in main
http_text = get_text(site_id)
File "test.py", line 110, in get_text
return get_url(url)
File "test.py", line 59, in get_url
except timingout:
**TypeError: catching classes that do not inherit from BaseException is not allowed**
在此先感谢您的任何建议和帮助!
是试图用timingout
捕捉异常造成的。 timingout
是一个整数对象,而 except
语句只接受从 BaseException
class.
继承的对象
删除那个 except
因为它什么都不做。还可以考虑修改 try
语句以仅包含一个操作。这将使故障排除更加容易,并防止您在以后出现错误时不得不分解您的 try
语句。
我有点 Python 新手,这是我第一次 post Whosebug 所以请多多包涵。 :) 在 posting 之前,我搜索了 google 和 Whosebug,但似乎找不到与我的问题类似的内容。
我有一个脚本可以轮询网站并检索内容。 它可以正常工作数小时,但是如果它遇到套接字超时,即使我有一个例外,脚本也会抛出类型错误。
我确定我遗漏了一些明显的东西,但我无法确定。
代码:
timingout = 10
def get_url(url):
try:
sock = urllib.request.urlopen(url, timeout=timingout)
orig_html = sock.read()
html = orig_html.decode("utf-8", errors="ignore").encode('cp1252', errors='ignore')
sock.close()
except KeyboardInterrupt:
# Kill program if Control-C is pressed
sys.exit(0)
except urllib.error.URLError as e:
print("***Error= Page ", e.reason)
return
except timingout:
print("socket timed out - URL: %s", url)
else:
# See if site is Down or errors eg: 404
if html == None:
print ("page contains no content!?!")
return ''
# See if site is complaining
elif html == site_overload:
if _verbose:
print('ERROR: Too many requests - SLEEPING 600 secs')
time.sleep(600)
return ''
# If not, we are good
elif html:
return html
错误:
return self._sock.recv_into(b)
**socket.timeout: timed out**
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 201, in <module>
main()
File "test.py", line 140, in main
http_text = get_text(site_id)
File "test.py", line 110, in get_text
return get_url(url)
File "test.py", line 59, in get_url
except timingout:
**TypeError: catching classes that do not inherit from BaseException is not allowed**
在此先感谢您的任何建议和帮助!
是试图用timingout
捕捉异常造成的。 timingout
是一个整数对象,而 except
语句只接受从 BaseException
class.
删除那个 except
因为它什么都不做。还可以考虑修改 try
语句以仅包含一个操作。这将使故障排除更加容易,并防止您在以后出现错误时不得不分解您的 try
语句。