异常处理请求通过SSLError

时间:2017-12-26 07:53:37

标签: python error-handling

这是我检查链接的功能。但是当链接是假的时,它会抛出一个错误。例如,它适用于twitter.com但不适用于twitt.com。

class Quality_Check:
    def check_broken_link(self,data):
        url= requests.head(data)

        try:
            if url.status_code==200 or url.status_code==302 or url.status_code==301:
                return True
        except requests.exceptions.SSLError as e:
            return False

qc=Quality_Check()
print(qc.check_broken_link('https://twitte.com'))

当我尝试通过此方法处理异常时,它显示以下错误:

Traceback (most recent call last):
      ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

在处理上述异常期间,发生了另一个异常:

raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='twitte.com', 
port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, 
'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)'),))

另一个也出现了

requests.exceptions.SSLError: HTTPSConnectionPool(host='twitte.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)'),))

1 个答案:

答案 0 :(得分:4)

if ( ! class_exists('WC_Email_Customer_Order_Verify') ) { class WC_Email_Customer_Order_Verify extends WC_Email { /** * Constructor */ function __construct() { // Triggers for this email add_action( 'woocommerce_order_status_order-verify', array( $this, 'trigger' ) ); add_action( 'woocommerce_checkout_order_processed', array( $this, 'trigger' ) ); // Call parent constructor parent::__construct(); } /** * Trigger. * @param WC_Order|int $wcOrder * @throws InvalidOrderException */ function trigger( $wcOrder ) { if ( ! $this->is_enabled() || ! $this->get_recipient() ) { return; } $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); } /** * get_content_html function. * * @access public * @return string */ function get_content_html() { ob_start(); wc_get_template( $this->template_html, array( 'order' => $this->object, 'orderDetails' => $this->orderDetails, 'sent_to_admin' => false, 'plain_text' => false ) ); return ob_get_clean(); } /** * get_content_plain function. * * @access public * @return string */ function get_content_plain() { ob_start(); wc_get_template( $this->template_plain, array( 'order' => $this->object, 'orderDetails' => $this->orderDetails, 'sent_to_admin' => false, 'plain_text' => true ) ); return ob_get_clean(); } } } return new WC_Email_Customer_Order_Verify(); 行发生异常。因此,您应该在url= requests.head(data)中包含该行,如下所示:

try

返回class Quality_Check: def check_broken_link(self,data): try: url = requests.head(data) if url.status_code == 200 or url.status_code == 302 or url.status_code == 301: return True except requests.exceptions.SSLError as e: return False qc=Quality_Check() qc.check_broken_link('https://twitte.com') ,并在False上返回'https://twitter.com',这是所需的结果。

顺便说一下,你可以改变你的

True

if url.status_code == 200 or url.status_code == 302 or url.status_code == 301:
相关问题