如何WMS查询时除外TimeOutError?

问题描述 投票:0回答:1

我需要你的建议。我建立与WMS(Web地图服务)的连接。如果连接已经建立,WMS添加在matplot窗口中绘制。在大多数情况下,正确地显示从WMS图像。但是,也有(我附后)当从WMS加载图像太长,这会导致错误的情况。这通常是超时错误。

我想带我的代码中,在当超过超时时刻从WMS图像停止加载形式。我想避免图像再次请求。

为此,我想除了TimeError。不幸的是,我不能。当然,我试过

除了:

方法(没有指定错误),但它不工作。下面的代码也不起作用。

try:
    resp = requests.get('http://mapy.geoportal.gov.pl/wss/service/img/guest/ORTO/MapServer/WMSServer')
    odpowiedz_wms = resp.status_code except:
    resp.status_code = 0 if resp.status_code == 200:
    wms = ax.add_wms(wms='http://mapy.geoportal.gov.pl/wss/service/img/guest/ORTO/MapServer/WMSServer', layers=['Raster'])
    try:
        plt.draw()
    except (requests.exceptions.RequestException,socket.timeout,timeout, exceptions.ConnectionError,ReadTimeout, TimeoutError, exceptions.Timeout,exceptions.ReadTimeoutError,ReadTimeoutError,exceptions.ConnectTimeout, exceptions.ReadTimeout, requests.exceptions.ReadTimeout, urllib3.exceptions.ReadTimeoutError, socket.timeout) as e:
        wms.remove()
        plt.draw()

例外的Tkinter回调回溯(最近通话最后一个):(...)

文件 “C:\ Python35 \ LIB \ socket.py”,线路575,在readinto回报self._sock.recv_into(B)socket.timeout:超时

在处理上述异常,另一个异常:

回溯(最近通话最后一个):(......)文件 “C:\站点包\ urllib3 \ connectionpool.py Python35 \ LIB \”,线路306,在_raise_timeout提高ReadTimeoutError(自我,网址,“读超时。 (读取超时=%S)”%timeout_value)urllib3.exceptions.ReadTimeoutError:HTTPConnectionPool(主机= 'mapy.geoportal.gov.pl',端口= 80):读超时。 (读取超时= 30)

在处理上述异常,另一个异常:

回溯(最近通话最后一个):(......)R = adapter.send(请求,** kwargs)文件 “C:\ Python35 \ LIB \站点包\请求\ adapters.py”,线路526,在发送提高ReadTimeout(即,请求=请求)requests.exceptions.ReadTimeout:HTTPConnectionPool(主机= 'mapy.geoportal.gov.pl',端口= 80):读超时。 (读取超时= 30)

python-3.x matplotlib timeout wms cartopy
1个回答
0
投票

当一个异常中的一段代码抛出

  • 堆栈会放松,直到它达到一个异常处理程序(尝试 - 除了块)
  • 如果另一异常处理异常(在除块代码)中提出的。那么栈将进一步放松,以能够捕捉异常的下一个异常处理程序。现在需要处理的例外是不是原来的例外。现在是最后一个异常升高
  • 如果调用堆栈解开到如此地步,没有例外处理程序进一步调用堆栈则该过程将终止与堆栈跟踪将倾显示在屏幕上显示的所有错误
  • 许多图书馆包含记录器,这将处理异常时打印出堆栈跟踪。在这种情况下,你仍然会得到转储到屏幕的堆栈跟踪。但例外不一定会终止进程。它可以被处理。
  • 这是很好的做法来实现记录在您的异常处理程序,让你知道哪些异常存在的,因此您可以调试,如果出现错误
  • 在这种情况下,你的代码将不得不处理当前异常是没有第一个抛出的异常。这是一个需要被处理的ReadTimeout。虽然,除了使用不带参数是抓什么,来了一个好办法。异常处理这个方法必须谨慎,并只能作为最后的手段使用。它经常会赶上那是你绝对不希望赶上真正的错误例外。总是试图指定你居然打算在捕获异常的类型

Example

try:
    try:
        1/0
    except ZeroDivisionError:
        # Catches the ZeroDivisionError
        raise IndexError
except IndexError:
    # Catches the IndexError - Doesn't need to catch the ZeroDivisionError
    raise NameError # raises unhandled exception. Process terminated and entire stack trace will be printed

Output

Traceback (most recent call last):
   File "pyex.py", line 14, in <module>
     1/0
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "pyex.py", line 17, in <module>
    raise IndexError
IndexError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "pyex.py", line 21, in <module>
    raise NameError # raises unhandled exception. Entire stack trace 
will be printed out
NameError
© www.soinside.com 2019 - 2024. All rights reserved.