python - 如何在 GAE 上捕获 Python 中的 DNS 查找失败错误?

标签 python google-app-engine error-handling dns urlfetch

我使用 urlfetch 测试人们提供的 URL,以捕获错误的链接。

result = urlfetch.fetch(url)

当我提供诸如“http://qwerty.uiop”之类的 URL 时,日志显示存在“DNSLookupFailedError”,但此代码无法捕获它:

except urlfetch.DNSLookupFailedError:
self.error(400)
self.response.out.write(
  'Sorry, there was a problem with URL "' + url + '"')

我还尝试了“除了 urlfetch.Error:”和“除了 urlfetch.DownloadError:”

我做错了什么,还有其他方法可以完成我想做的事情吗?

最佳答案

在本地开发人员环境和生产环境中,我实际上看到了不同的异常:DownloadError。捕获这一点对我来说效果很好。

try:
  result = urlfetch.fetch('http://qwerty.uiop')
except urlfetch.DownloadError:
  self.response.write('Oops!')

关于python - 如何在 GAE 上捕获 Python 中的 DNS 查找失败错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13318645/

相关文章:

python - 从颜色直方图生成随机图像的最快方法是什么?

python - 显示维恩图输出

python - 从 C 调用 IPython.embed() 方法时为 "ValueError: call stack is not deep enough"

python - 当前在 Google App Engine 上存储、索引和查询地理空间数据的选项有哪些?

php - App Engine PHP 上传错误

perl - 在 Test::More 中的 eval 期间保护全局变量

python - openpyxl 将数字格式设置为单元格但不起作用

google-app-engine - 尝试在本地上传到 Blobstore 时出现 OutOfMemoryError

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

php - PHP//死亡白屏,报告所有错误