【问题标题】:How to automate python scripts on a web apache server如何在 web apache 服务器上自动化 python 脚本
【发布时间】:2021-12-11 18:25:18
【问题描述】:

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

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

【问题讨论】:

  • 您可以将cronjob 设置为每 30 分钟、1 小时或任何您想要的。
  • 谢谢。但正如我所说,这似乎是过度杀戮。每次有新的网络订单时,我都需要运行脚本。
  • 我强烈推荐celery
  • @wakeZheng 谢谢你我会去看看。

标签: python python-3.x automation


【解决方案1】:

这是您无需自动化的用户下订单时可以调用此代码的代码

如果你使用的是 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"

【讨论】:

    猜你喜欢
    • 2019-11-17
    • 2018-01-30
    • 2010-12-11
    • 1970-01-01
    • 2020-07-25
    • 2014-12-03
    • 2014-03-27
    • 2016-12-08
    相关资源
    最近更新 更多