如何使用Nominatim处理错误?

时间:2014-09-01 13:01:26

标签: python error-handling

我正在通过geopy运行Nominatim网络服务,但由于使用政策或互联网连接,它经常失败。如何处理与停止的失败连接,并在几秒钟或几分钟后重新运行代码。错误消息是:

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>

GeocoderServiceError: HTTP Error 420: unused

伪代码如下:

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

任何暗示或建议都是最受欢迎的。

谢谢!

1 个答案:

答案 0 :(得分:0)

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

基于geopy文档(http://geopy.readthedocs.org/en/latest/#exceptions),使用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