简体   繁体   English

urllib3连接失败是什么类型的异常?

[英]What type of exception is a urllib3 connection failure?

I have a section of code that gives me a urllib3 error when the IP address of the host it's connecting to is wrong, and also stops the App from starting up. 我有一段代码,当它所连接的主机的IP地址错误时,会给我一个urllib3错误,并且还会阻止该应用程序启动。

This connection attempt is in a view(not the main view), which I'm assuming is being called when the application starts up. 这种连接尝试是在一个视图(不是主视图)中进行的,我假设在应用程序启动时被调用。

I want to write a try to check for this exception, however, the error doesn't mention what exception type it's seeing. 我想编写一个try检查此异常的try ,但是,该错误未提及它所看到的异常类型。

Q: What Exception type can I specify in the try to handle this error when then application starts up? 问:应用程序启动后,在try处理此错误时可以指定哪种异常类型?

Error 错误

Was unable to import app 无法导入应用
Error: HTTPSConnectionPool(host='10.0.0.1', port=834): Max retries exceeded with url: /items (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 113] No route to host',)) 错误:HTTPSConnectionPool(host = '10 .0.0.1',端口= 834):url:/ items超过了最大重试次数(由NewConnectionError(':导致无法建立新连接:[Errno 113]没有通往主机的路由,“) )

Thanks in advance. 提前致谢。

Thanks @cricket_007 谢谢@ cricket_007

solved it by importing the exceptions for requests. 通过导入请求的异常来解决它。

from requests.exceptions import ConnectionError

and then trigger in the try statement. 然后在try语句中触发。

try:
    <<something>>
except ConnectionError:
    <<something>>

In answer to the question actually asked, you can find out the base class(es) of a Python class via its __bases__ tuple. 为了回答实际提出的问题,您可以通过其__bases__元组找到Python类的基类。 For example, 例如,

>>> class MyZeroDivide( ZeroDivisionError):
...   pass
>>> z = MyZeroDivide()  # instance
>>> z.__class__        
<class '__main__.MyZeroDivide'>  # class
>>> z.__class__.__bases__
(<type 'exceptions.ZeroDivisionError'>,)  # base class(es)
>>> z.__class__.__bases__[0].__bases__
(<type 'exceptions.ArithmeticError'>,)    # up a level
>>> z.__class__.__bases__[0].__bases__[0].__bases__
(<type 'exceptions.StandardError'>,)      # up two levels

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM