如何在web apache服务器上自动化python脚本



我想在python中创建一个自动脚本,当一个新的订单放在我的网站上时,会用电子邮件通知我。到目前为止,我发现我可以每分钟使用apache服务器上的cronjobs,但这似乎是多余的,因为我并不是每分钟都收到订单。

所以我在寻找一个更好的解决方案。

这是代码你可以在用户下订单时调用这段代码你不需要自动执行

如果你使用的是django,你可以这样写:

from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
fail_silently=False,
)

如果没有django,你可以这样写:

import smtplib
sender = 'from@example.com'
receivers = ['to@example.com']
message = “This is a test e-mail message."
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)         
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"

最新更新