如何将 python 脚本的输出发送到电子邮件地址
我有一个线程 python 脚本,可以 ping 局域网上的 20 个节点,并打印出每个节点的状态:节点处于活动状态、节点处于关闭状态等。 我想将此输出发送到我的电子邮件帐户,因为我打算让这个脚本每周运行一次,单独运行,如果我物理上远离局域网,我不必担心,我可以只需查看我的电子邮件即可。
语言:Python。操作系统:Linux Mint 10 Julia。谢谢
I have a threaded python script that pings 20 nodes on a local area network, and prints out the status of each: Node is Alive, Node is Down, etc..
I would like to have this output sent to my email account, Because I intend to make this script to run once a week, on it's own, And if i'm physically away from the lan I won't have to worry, I can just check my email.
Language:PYTHON. OS:Linux Mint 10 Julia. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果它每周运行一次,您可能会从 crontab 运行它?
If it runs once a week, you will probably run it from crontab?
使用 smtplib。他们提供的 示例 非常好。
Use smtplib. The example they provide is pretty good.
您可以使用
logger
来打印到 stout 和记录器,而不是使用主print
输出。您可以根据 Logging Cookbook:
现在替换脚本中的
print
使用logger.info
之前的示例:
之后的示例:
然后您可以将日志通过电子邮件发送给自己,如下所示:
Instead of having your main
print
the output, you could use alogger
to print to stout and to the loggerYou can set up a logger as follows according to the Logging Cookbook:
Now in replace
print
in your script withlogger.info
Example before:
Example after:
Then you can email the log to yourself as follows:
看一下logging和logging.config,我之前用过它从后台运行的脚本接收错误消息
http://docs.python.org/library/logging.html
例如
然后是logging.conf
从上面我会在我的电子邮件中收到“这是一个错误”。
Take a look at the logging and logging.config, I've used this before to receive error messages from a script running in the background
http://docs.python.org/library/logging.html
For example
And then the logging.conf
From the above I would receive "THIS IS AN ERROR" in my email.
Al Sweigart 的“用 Python 自动化无聊的工作”是关于使用 Python 发送电子邮件主题的一个很好的资源。 第 18 章 是您想要的部分。简而言之,如果您有一个大型电子邮件提供商(例如 Google、Outlook、Yahoo 等)的电子邮件地址,您可以使用他们的简单邮件传输协议 (SMTP) 服务器来处理来自 Python 的消息。正如 Al 所说:
如果您没有大型提供商的电子邮件地址,或者您无法使用外部提供商的电子邮件地址,那么这是一个难一点。也许您公司的某个人可以告诉您 1) 您的公司是否有 SMTP 服务器以及 2) 它的域名和端口号是什么。
一旦你拥有了所有这些,从你的程序发送一封电子邮件就是小菜一碟:
记录器也很棒,所以不要低估每个人对它们的评价。
打印到终端。记录到文件。并通过电子邮件发出!记录仪可以做一切事情。
A great resource on the topic of sending email with Python is Al Sweigart's 'Automate the Boring Stuff with Python'. Chapter 18 is the part you want. In short, if you have an email address with one of the big email providers (think Google, Outlook, Yahoo, etc.) you can use their Simple Mail Transfer Protocol (SMTP) server(s) to handle your messages from Python. As Al says:
If you don't have an email address with one of the big providers or you're not in a position where you can use an email address from an outside provider, then it's a bit harder. Perhaps someone at your company can tell you 1) if your company has an SMTP server and 2) what its domain name and port number are.
Once you have all that, sending out an email message from your program is a piece of cake:
Loggers are great too, so don't discount what everyone's said about them.
Print to the terminal. Log to a file. AND email out! Loggers can do everything.
您需要一个 SMTP 服务器来发送电子邮件。查看 Python 的 smtplib
You need a SMTP server to send Email. Check out the smtplib for Python