如何检查网站是否正常工作时如何绕过或捕获socket.timeout错误?

时间:2019-02-28 05:19:50

标签: python python-3.x

我一直在开发一个程序,以检查网站是否正常运行。我从Excel工作表中获取URL,然后在同一Excel工作表中将结果粘贴为True和false,但是对于某些URL,我遇到了socket.timeout错误,此后代码无法正常工作。这是代码:

   import http.client as httpc
from urllib.parse import urlparse
import pandas as pd
import xlwings as xw
import smtplib
from xlsxwriter import Workbook


import socket


x=[]

df = pd.read_excel (r'xyz.xlsx')
df1=pd.DataFrame(df,columns=['URL'])
print(df1)
url_list=df["URL"].tolist()
print(url_list)
for i in url_list:
    def checkUrl(i):
        if 'http' not in i:
            i= 'https://'+i
        p = urlparse(i)
        conn = httpc.HTTPConnection(p.netloc,timeout=4)
        conn.request('HEAD', p.path)
        try:
            resp = conn.getresponse()
            return resp.status<400
        except requests.exceptions.RequestException:
            return False
    print(checkUrl(i))
    x.append(checkUrl(i))


workbook = Workbook('abc.xlsx')
Report_Sheet = workbook.add_worksheet()
Report_Sheet.write(0, 1, 'Value')
Report_Sheet.write_column(1, 1, x)

workbook.close()

3 个答案:

答案 0 :(得分:1)

此代码中有很多问题。

  1. 即使URL需要HTTPS,您也可以无条件使用HTTP
  2. 您执行try:的请求撤消
  3. except子句希望您的代码不能抛出requests.exceptions.RequestException

由于您不是在使用请求库,而是在较低级别的http.client上使用,因此您只应该期待套接字库中的错误,而这些错误都是OSError的子类

您的代码可能会(请注意:未经测试):

def checkUrl(i):
    if 'http' not in i:
        i= 'https://'+i
    p = urlparse(i)
    if (p.scheme == 'http'):
        conn = httpc.HTTPConnection(p.netloc,timeout=4)
    else:
        conn = httpc.HTTPSConnection(p.netloc,timeout=4)
    try:
        conn.request('HEAD', p.path)
        resp = conn.getresponse()
        return resp.status<400
    except OSError:
        return False

答案 1 :(得分:1)

以我的经验,当IP地址解析为有效的主机名但服务器不再配置为使用该主机名时,会发生此错误。这导致服务器忽略您尝试连接到它的尝试。

要处理此问题,您应该在超时错误时返回False。

    import socket

    try:
        resp = conn.getresponse()
        return resp.status<400
    except requests.exceptions.RequestException:
        return False
    except socket.timeout as err:
        return False

您将要检查http.client.HTTPException而不是requests.exceptions.RequestException,因为您所做的检查使用的是http.client库而不是requests库。此外,您还将希望捕获以下所有错误。

    import socket
    import ssl
    import http.client

    try:
        resp = conn.getresponse()
        return resp.status < 400
    except http.client.HTTPException as err:
        # A connection was established, but the request failed
        return False 
    except socket.timeout as err:
        # The website no longer exists on the server
        return False
    except socket.gaierror as err:
        # Could not resolve the hostname to an IP address
        return False
    except ssl.CertificateError as err:
        # The SSL certificate was never configured, or it cannot be trusted
        return False
    except ssl.SSLError as err:
        # Other SSL errors not covered by ssl.CertificateError
        return False

答案 2 :(得分:0)

第一个猜测是

resp = conn.getresponse()

应该在try子句中。如果不起作用,请添加程序的输出。