免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4419 | 回复: 1
打印 上一主题 下一主题

初学PYTHON,求指导mysql日期字段查询后与当前时间做比较问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-11-14 17:31 |只看该作者 |倒序浏览
目前有个mysql库
dbname=myserver
tablename=PhysicalDevice

select name,end_of_warranty from PhysicalDevice

+--------------------------------------------------+-----------------+
| name                                             | end_of_warranty |
+--------------------------------------------------+-----------------+
| Server1                                          | NULL            |
| Server2                                          | NULL            |
| Server3                                          | NULL            |
| Server4                                          | 2013-05-21      |
| Router1                                          | NULL            |
| Switch1                                          | NULL            |
| Rack1                                            | NULL            |
| Rack B01                                         | 2015-03-28      |
| Rack B02                                         | 2015-03-28      |
| Rack B03                                         | 2015-03-28      |
| Rack B04                                         | 2015-03-28      |
| VCENTER SERVER                                   | 2015-02-01      |
| livep1                                           | 2015-02-01      |
| livep2                                           | 2015-02-01      |
| livep3                                           | 2015-02-01      |
| livep4                                           | 2015-02-01      |
| livep5                                           | 2015-02-01      |
+--------------------------------------------------+-----------------+


想用PYTHON实现 对比end_of_warranty与当前时间,删选出维保三个月内到期的条目,并发出邮件通知,有大大能指导下吗

论坛徽章:
4
白羊座
日期:2013-11-05 10:26:09冥斗士
日期:2015-11-17 14:19:55白银圣斗士
日期:2015-11-17 15:13:0815-16赛季CBA联赛之新疆
日期:2016-04-01 09:10:58
2 [报告]
发表于 2013-11-14 23:06 |只看该作者
个人想法,如果数据库的压力没有那么大,可以考虑使用datediff用sql得出有哪些需要被通知,然后,就只需要用python去一条条发邮件通知去了。
至于用python去发邮件,下面是找来的一个例子。
  1. #!/usr/bin/python

  2. import smtplib

  3. sender = 'from@fromdomain.com'
  4. receivers = ['to@todomain.com']
  5. message = """From: From Person <from@fromdomain.com>
  6. To: To Person <to@todomain.com>
  7. MIME-Version: 1.0
  8. Content-type: text/html
  9. Subject: SMTP HTML e-mail test

  10. This is an e-mail message to be sent in HTML format

  11. <b>This is HTML message.</b>
  12. <h1>This is headline.</h1>
  13. """

  14. try:
  15.    smtpObj = smtplib.SMTP('localhost')
  16.    smtpObj.sendmail(sender, receivers, message)         
  17.    print "Successfully sent email"
  18. except SMTPException:
  19.    print "Error: unable to send email"
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP