简体   繁体   English

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

[英]How to automate python scripts on a web apache server

I would like to create an automatic script in python that will inform me with an email when a new order is place on my website.我想在 python 中创建一个自动脚本,当我的网站上有新订单时,它会通过电子邮件通知我。 So far i found i can use cronjobs from apache server every minute but it seems to be an overkill since i dont get orders every minute.到目前为止,我发现我每分钟都可以使用来自 apache 服务器的 cronjobs,但这似乎有点矫枉过正,因为我每分钟都没有收到订单。

So im looking for a better solution.所以我正在寻找更好的解决方案。

Here's the code you can call this code whenever the order is placed by the user you don't need to automate这是您可以在不需要自动化的用户下订单时调用此代码的代码

If you are using django you can use this:如果您使用的是 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,
)

Without django you can use this :没有 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM