zoukankan      html  css  js  c++  java
  • 【python】一个备份把文件备份到邮箱的python实现

    公司服务器弄了跳板机,从服务器上拉文件变得好麻烦,弄了个脚本从服务器上向邮箱发送文件,还蛮方便哈~

    #!/usr/bin/env python2.7
    #! coding:UTF-8
    
    
    import smtplib
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
    from email.MIMEText import MIMEText
    from email.Utils import COMMASPACE, formatdate
    from email import Encoders
    import os
    import sys
    from optparse import OptionParser
    
    g_username = "123123123"
    g_password = "xxxxxxxxxxx"
    
    def send_mail(to,subject,content,attached_files):
        smtp = smtplib.SMTP()
        smtp.set_debuglevel(1)
        smtp.connect("smtp.qq.com","25")
        smtp.login(g_username, g_password)
        msg = MIMEMultipart()
        msg["From"] = g_username + "@qq.com"
        msg["To"] = to
        msg['Date'] = formatdate(localtime=True)
        content = "[backup] files: "+ ",".join(attached_files)
        msg['Subject'] = content[:32]
        msg.attach(MIMEText(content))
        for filename in attached_files:
            if not os.path.exists(filename):
                continue
            part = MIMEBase('application', "octet-stream")
            part.set_payload(open(filename,"rb").read())
            Encoders.encode_base64(part)
            part.add_header('Content-Disposition', 'attachment; filename="%s"' 
                            % os.path.basename(filename))
            msg.attach(part)
        smtp.sendmail(g_username + '@qq.com',[to],msg.as_string())
        smtp.close()
    
    if __name__ == "__main__":
        parser = OptionParser()
        (option,args) = parser.parse_args()
        send_mail("接受备份文件的邮箱@163.com","trival","trival",args)
    
  • 相关阅读:
    GoWeb-Gin 文件上载
    Node.JS + Mysql数据库
    Node.JS 项目打包 JXCore
    Express web框架 upload file
    hdoj 4430 Yukari's Birthday
    hdoj 4282 A very hard mathematic problem
    hdoj 3750 Guess Game
    hdoj 2199 Can you solve this equation?
    uva 11384 Help is needed for Dexter
    ios中fixed元素在滚动布局中的延时渲染问题
  • 原文地址:https://www.cnblogs.com/igloo1986/p/3282594.html
Copyright © 2011-2022 走看看