Python 如何使用提名来处理错误?

Python 如何使用提名来处理错误?,python,error-handling,Python,Error Handling,我正在运行geopy提供的Nammitm web服务,但由于使用策略或Internet连接,该服务经常失败。我如何处理连接失败时停止,并在几秒钟或几分钟后重新运行代码。错误消息是: GeocoderServiceError: <urlopen error [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time,

我正在运行geopy提供的Nammitm web服务,但由于使用策略或Internet连接,该服务经常失败。我如何处理连接失败时停止,并在几秒钟或几分钟后重新运行代码。错误消息是:

GeocoderServiceError: <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>
伪代码如下所示:

try:
    run web service
except:
    stop several seconds or minutes and rerun webservice at the same line and loops
(if it fails again)
    stop 30 minutes and rerun webservice
任何提示或建议都是最受欢迎的


谢谢

感谢您的上述评论。修改try/except是解决办法

根据geopy文档(),使用geopy最常见的异常是GeocoderServiceError。下面是处理错误的修订代码

try:
    run web service
except geopy.exc.GeocoderServiceError as e:
    if e.message == 'HTTP Error 420: unused':
        time.sleep(1800)
        run web service
    elif e.message == '<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>':
        time.sleep(5)
        run web service
试试看:
运行web服务
除geopy.exc.GeocoderService错误为e外:
如果e.message==“HTTP错误420:未使用”:
时间。睡眠(1800)
运行web服务
elif e.message=='':
时间。睡眠(5)
运行web服务

我想你应该弄清楚,为什么它经常掉下来。这不应该发生。试着不要使用像
这样的短语,我想知道
-而是问一个问题,比如
我怎么能
这很肤浅,只是把这段代码放在
循环中,同时也试着/除了不“忽略错误”相反,你需要知道你想要捕获哪个错误以及如何处理它,except标记不会在同一次尝试中工作两次,除非它不是一个接一个,而是except1或except2或。。。。例外。
try:
    run web service
except geopy.exc.GeocoderServiceError as e:
    if e.message == 'HTTP Error 420: unused':
        time.sleep(1800)
        run web service
    elif e.message == '<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>':
        time.sleep(5)
        run web service