今天你存钱了吗

作者: 我的学长是王欣 | 来源:发表于2017-02-07 14:46 被阅读321次

    2017存钱计划(python实现)


    灵感来源于在微博看到的一条微博,365天存钱法,每天从1到365中任选一个数字存钱,每一天的数字都不能重复,这样一年下来就有66795元,6W多块钱对于我来说太多,那就从0.1-36.5,根据收入多寡,每天选择其中的一个数字来存,一年就有6679.5元,是一笔不少的数字。那就开始吧。

    • 考虑到之前租借的vps利用率不高,就打算用vps中安装的centos的定时任务crontab,配合上sendmail来发送邮件告知每日应该存多少钱。
    1. 安装Xshell,连接上远端服务器。 yum -y install sendmail crontabs 安装必要的软件。

    2. touch sendmail.py vim sendmail.py

    #!/usr/bin/python
    #-*-coding:utf-8 -*-  //指定编码,兼容中文
    import random
    import os
    import time
    import datetime
    
    # the remainder days of next China Year
    today = datetime.date.today()
    other_day = datetime.date(2018,2,15)
    remainder = other_day - today
    
    # 每日存钱的数字
    tar = random.randint(1,365)
    
    result = []
    with open('/root/saveMoney/saved.txt','r') as f:
        data = f.readlines()
        for line in data:
            tmp = line.split()
            for x in tmp:
                result.append(x)
    num_int = map(float,result)
    while str(tar) in result:
        tar = random.randint(1,365)// 保证产生的数不重复
        
    # 保存已经存过的数字到saved.txt 
    with open('/root/saveMoney/saved.txt','a') as f:
        f.write(' ')
        f.write(str(tar))
        f.close()
    # 要发送的文本写入到todo.txt
    with open('/root/saveMoney/todo.txt','w') as f:
        f.write("今天,Centos6建议您存入" + str(tar/10.0) + "元\n")
        f.write("距离过年还有" + str(remainder.days) + "天")
        f.close()
    
    #调用mail外部命令发送邮件
    os.system(" mail -s 'save money plant' xx@xx.com < /root/saveMoney/todo.txt")
    
    1. 新建 crontab.cron vim crontab.cron
      24 10 * * * python /root/saveMoney/sendmail.py 达到每天10点24分定时发送的目的。
    2. crontab crontab.cron
    3. 虽然可以不重新启动服务,但为了确保crond正常工作,还是加入 service crond restart
    4. 到此,结束。

    相关文章

      网友评论

        本文标题:今天你存钱了吗

        本文链接:https://www.haomeiwen.com/subject/vptmittx.html