zoukankan      html  css  js  c++  java
  • 发送动态IP到邮件

    #  -*-coding:utf8 -*-
    #!/usr/bin/python
    
    import smtplib
    from email.mime.text import MIMEText
    
    # IP
    import urllib2
    import re
    
    # 时间函数
    import time
    
    # 随机整数
    import random
    
     
    # 第三方 SMTP 服务
    mail_host = "smtp.qq.com"  # SMTP服务
    mail_user = "1124760XXX@qq.com"  # 用户名
    mail_pass = "pxzvevrxxxbage"  # 密码 
     
    sender = '1124760XXX@qq.com'  # 发件人邮箱
    receivers = ['pxxx@163.com']  # 接收人邮箱
     
    # 公网IP函数
    def changeip( ):
        
        req = urllib2.Request("http://txt.go.sohu.com/ip/soip")
        
        try:
            url = urllib2.urlopen(req, data=None, timeout=3)
            text = url.read()
            ip = re.findall(r'd+.d+.d+.d+',text)    
            ip=ip[0]
            return ip
        
        except urllib2.HTTPError,e:
            print e.code
    
        
         
    
    # 邮件函数
    def changeemail(ip):
        content = ip
        title = '公网IP'  # 邮件主题
        message = MIMEText(content, 'plain', 'utf-8')  # 内容, 格式, 编码
        message['From'] = "{}".format(sender)
        message['To'] = ",".join(receivers)
        message['Subject'] = title
        try:
            smtpObj = smtplib.SMTP_SSL(mail_host, 465)  # 启用SSL发信, 端口一般是465
            smtpObj.login(mail_user, mail_pass)  # 登录验证
            smtpObj.sendmail(sender, receivers, message.as_string())  # 发送
            smtpObj.quit();
            print '发送成功!'
        except smtplib.SMTPException as e:
            print(e)
    
    
    i = 0
    agoip = '11.1'  # 邮件主题
    while i < 1 :
        try:
            ip = changeip();
            print ip;
            print time.asctime( time.localtime(time.time()))
            if ip != agoip: changeemail(ip);
            agoip =  ip        
        except ip as e:
            print(e)
            print 'ip错误';   
        
        
        time.sleep(random.randint(600,1200))
    
  • 相关阅读:
    《链队列---队列的链式表示和实现》
    《栈的应用_版本1.2(实现了可以在一次运行后进行多次操作)》
    《栈的应用_版本1.1(实现了如何十进制转十六进制)》
    《栈的应用 版本1.0》
    《栈的基本操作》
    《单链表练习》
    hdu5887 Herbs Gathering
    CF198 div1 D
    hdu5893 List wants to travel
    hdu5556 Land of Farms
  • 原文地址:https://www.cnblogs.com/pxuan/p/10622605.html
Copyright © 2011-2022 走看看