[英]How to bypass or catch the error of socket.timeout when checking if a website is working or not?
我一直在开发一个程序来检查网站是否正常工作。 我从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()
这段代码有很多问题。
try:
的请求try:
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
根据我的经验,当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
首先猜测的是
resp = conn.getresponse()
应该在try子句中。 如果这不起作用,请添加程序的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.