[Python] python控制随机间隔一段时间发送邮件、消息或弹出窗口 →→→→→进入此内容的聊天室

来自 , 2020-10-23, 写在 Python, 查看 122 次.
URL http://www.code666.cn/view/b97f1389
  1. """ If you need to do something irregularly, randomly during the weekday, you
  2. often forget. This script gives you mail, sms or popup window indefinitely in
  3. random interval to remind you of doing it. It runs forever. If you want to
  4. send emails, uncomment the row sendMail() and fill variable me, to, smtp,
  5. name, login in function sendMail(). """
  6.  
  7. # -*- coding: utf-8 -*-
  8. import time, random, sys, os, smtplib
  9.  
  10. if sys.version < '3':
  11.     from Tkinter import *
  12. else:
  13.     from tkinter import *
  14.  
  15. # ----------------- variables ----------------------------------
  16. mytimefrom=6        # from 6 pm
  17. mytimeto=22         # to 22  am
  18. mydelayfrom=1*3600    # random delay in secs from
  19. mydelayto=5*3600      # random delay in secs to
  20. randommsgnext="randommsgnext.txt"     # place to save time of next message
  21.  
  22. # ----------------- defs ----------------------------------
  23.  
  24. def showMessage():
  25.     # show reminder message window
  26.     root=Tk()
  27.     x = (root.winfo_screenwidth() - root.winfo_reqwidth()) / 2
  28.     y = (root.winfo_screenheight() - root.winfo_reqheight()) / 2
  29.     root.geometry("+%d+%d" % (x, y))
  30.     root.protocol('WM_TAKE_FOCUS', root.update )
  31.     root.wait_visibility(root)
  32.     root.attributes('-topmost',1)
  33.     label=Label(root, text="Do what you should do.").pack({"side": "left"})
  34.     button=Button(text="OK", width="10", command=lambda:root.destroy()).pack()
  35.     root.mainloop()
  36.  
  37. def sendMail():
  38.     # sends mail
  39.     s = smtplib.SMTP(smtp)
  40.     s.login(name, password)
  41.  
  42.     subject="Reminder"
  43.     fromaddr=fromadr
  44.     toaddrs= [toadr]
  45.     text= "Do what you should do.\n%s" % time.ctime()
  46.     msg = ("Subject: %s\nFrom: %s\nTo: %s\n\n%s" % (subject, fromaddr, ", ".join(toaddrs), text))
  47.  
  48.     s.sendmail(fromaddr, toaddrs, msg)
  49.     s.quit()
  50.  
  51. def sendSMS():
  52.     # sends sms
  53.     message={'user': user, 'password': password, 'sender': fromnumber, 'recipient': tonumber, 'message': "goodbye"}
  54.     if sys.version < '3':
  55.         import urllib
  56.         params = urllib.urlencode(message)
  57.         f=urllib.urlopen(http, params)
  58.     else:
  59.         import urllib.parse
  60.         import urllib.request
  61.         params = urllib.parse.urlencode(message)
  62.         f = urllib.request.urlopen(http % params)
  63.  
  64. def checktime(nowtime):
  65.     # do not remind at night
  66.     if nowtime.tm_hour<=mytimefrom or nowtime.tm_hour>=mytimeto:
  67.         return False
  68.     return True
  69.  
  70. def timenextwritef():
  71.     # save next time in file
  72.     f=open(randommsgnext,"a")
  73.     timenext=time.mktime (nowtime)+delaysec
  74.     f.write(time.ctime(timenext)+"\n")
  75.     f.close()
  76.     os.utime(randommsgnext, None)
  77.  
  78. # ----------------- main app ----------------------------------
  79.  
  80. nowtime=time.localtime()
  81. os.chdir(os.path.dirname(sys.argv[0]))  # to have "randommsgnext" in the same dir
  82.  
  83. while True:
  84.     delaysec=random.randint(mydelayfrom,mydelayto)
  85.     timenextwritef()
  86.     time.sleep(delaysec)
  87.     nowtime=time.localtime()
  88.     if checktime(nowtime):
  89. #        sendSMS()
  90. #        sendMail()    # uncomment if you want sending mails
  91.         showMessage()
  92.  
  93. #//python/8165

回复 "python控制随机间隔一段时间发送邮件、消息或弹出窗口"

这儿你可以回复上面这条便签

captcha