如何创建到 SQL 服务器的数据库连接并将连接错误消息发送到 Python 中的电子邮件?
How do I create a database connection to SQL Server and send out connection error messages to an email in Python?
我已经尝试编写 Python 3 代码来连接到 SQL 服务器数据库,条件如下:-
- 如果数据库连接出现错误,则会在休眠 5 秒后重试连接。
- 目前最大重试次数为2次。可以更改为代码中的任何其他数字。
- 一旦达到最大重试次数但仍然存在连接错误,脚本会向我发送电子邮件通知我错误。
我的代码如下。它适用于最大尝试,但外观存在问题,以至于它开始不断发送电子邮件,直到我停止 运行 为止。看起来像一个无限循环,很可能在第二个 while 语句中。谁能指出问题出在哪里以及如何修复代码。
import configparser
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
import mimetypes
import email.mime.application
config = configparser.ConfigParser()
Driver='SQL Server'
config.read('connection.ini')
#Context is specific to my configuration.ini file that has the connection details
server = config['Context']['host']
db = config['Context']['database']
user = config['Context']['user']
password = config['Context']['pwd']
retry_flag = True
retry_count = 0
max_retries = 2
while retry_flag and retry_count < max_retries:
try:
cnxn_str = pyodbc.connect(driver=Driver, host=server, user=user,password=password, database=db)
retry_flag = False
except Exception as e:
error = str(e)
print(error)
print("Retry after 5 sec")
retry_count = retry_count+1
time.sleep(5)
while retry_count == max_retries:
smtp_ssl_host = config['Context']['smtp_ssl_host']
smtp_ssl_port = config['Context']['smtp_ssl_port']
email_user = config['Context']['email_user']
email_to = config['Context']['email_to']
email_pass =config['Context']['email_pass']
msg = MIMEMultipart()
msg['Subject'] = "Database Connection Error"
msg['From'] = email_user
msg['To'] = email_to
s = smtplib.SMTP_SSL(smtp_ssl_host, smtp_ssl_port)
s.login(email_user, email_pass)
txt = MIMEText("Database connection failed earlier. Please re-run the script manually.")
msg.attach(txt)
s.send_message(msg)
s.quit()
正确,您的第二个 while
永远运行,因为当您进入该循环时该条件始终为真(请勿更改 retry_counts
)。
您可以将其更改为 if 而不是 while,例如(我也加了一个try/catch)
if retry_count == max_retries:
try:
smtp_ssl_host = config['Context']['smtp_ssl_host']
smtp_ssl_port = config['Context']['smtp_ssl_port']
email_user = config['Context']['email_user']
email_to = config['Context']['email_to']
email_pass =config['Context']['email_pass']
msg = MIMEMultipart()
msg['Subject'] = "Database Connection Error"
msg['From'] = email_user
msg['To'] = email_to
s = smtplib.SMTP_SSL(smtp_ssl_host, smtp_ssl_port)
s.login(email_user, email_pass)
txt = MIMEText("Database connection failed earlier. Please re-run the script manually.")
msg.attach(txt)
s.send_message(msg)
s.quit()
except Exception as e:
print(e)
或者在最后增加 retry_counts
。不过,在我看来,这不会那么可读/“漂亮”。
我已经尝试编写 Python 3 代码来连接到 SQL 服务器数据库,条件如下:-
- 如果数据库连接出现错误,则会在休眠 5 秒后重试连接。
- 目前最大重试次数为2次。可以更改为代码中的任何其他数字。
- 一旦达到最大重试次数但仍然存在连接错误,脚本会向我发送电子邮件通知我错误。
我的代码如下。它适用于最大尝试,但外观存在问题,以至于它开始不断发送电子邮件,直到我停止 运行 为止。看起来像一个无限循环,很可能在第二个 while 语句中。谁能指出问题出在哪里以及如何修复代码。
import configparser
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
import mimetypes
import email.mime.application
config = configparser.ConfigParser()
Driver='SQL Server'
config.read('connection.ini')
#Context is specific to my configuration.ini file that has the connection details
server = config['Context']['host']
db = config['Context']['database']
user = config['Context']['user']
password = config['Context']['pwd']
retry_flag = True
retry_count = 0
max_retries = 2
while retry_flag and retry_count < max_retries:
try:
cnxn_str = pyodbc.connect(driver=Driver, host=server, user=user,password=password, database=db)
retry_flag = False
except Exception as e:
error = str(e)
print(error)
print("Retry after 5 sec")
retry_count = retry_count+1
time.sleep(5)
while retry_count == max_retries:
smtp_ssl_host = config['Context']['smtp_ssl_host']
smtp_ssl_port = config['Context']['smtp_ssl_port']
email_user = config['Context']['email_user']
email_to = config['Context']['email_to']
email_pass =config['Context']['email_pass']
msg = MIMEMultipart()
msg['Subject'] = "Database Connection Error"
msg['From'] = email_user
msg['To'] = email_to
s = smtplib.SMTP_SSL(smtp_ssl_host, smtp_ssl_port)
s.login(email_user, email_pass)
txt = MIMEText("Database connection failed earlier. Please re-run the script manually.")
msg.attach(txt)
s.send_message(msg)
s.quit()
正确,您的第二个 while
永远运行,因为当您进入该循环时该条件始终为真(请勿更改 retry_counts
)。
您可以将其更改为 if 而不是 while,例如(我也加了一个try/catch)
if retry_count == max_retries:
try:
smtp_ssl_host = config['Context']['smtp_ssl_host']
smtp_ssl_port = config['Context']['smtp_ssl_port']
email_user = config['Context']['email_user']
email_to = config['Context']['email_to']
email_pass =config['Context']['email_pass']
msg = MIMEMultipart()
msg['Subject'] = "Database Connection Error"
msg['From'] = email_user
msg['To'] = email_to
s = smtplib.SMTP_SSL(smtp_ssl_host, smtp_ssl_port)
s.login(email_user, email_pass)
txt = MIMEText("Database connection failed earlier. Please re-run the script manually.")
msg.attach(txt)
s.send_message(msg)
s.quit()
except Exception as e:
print(e)
或者在最后增加 retry_counts
。不过,在我看来,这不会那么可读/“漂亮”。