zoukankan      html  css  js  c++  java
  • python操作http,smtp协议。

    需要帮人写一段代码,代码很简单,就是读写文件,加上操作http和smtp,自然选择了库一大堆的python实现。

    代码如下。感谢google。

    gmail的貌似要调用starttls函数才能成功。

    import os
    import sys
    import urllib
    import httplib
    import string
    import smtplib
    import email
    import mimetypes
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText
    from email.utils import COMMASPACE,formatdate
    
    import smtplib
    
    def getRemoteIp(httpurl):
        page = urllib.urlopen(httpurl)
        if 200 != page.getcode():
            return None
        strContent = page.read()
        i = string.find(strContent,"[")
        if i == -1:
                return None
        j = string.find(strContent,"]",i)
        if j == -1:
                return None
        return strContent[i+1:j]
    
    def readFile(filename):
        file = open(filename,'rb')
        if file == None:
            return ""
        strIp = file.readline()
        file.close()
        return strIp
    
    def writeFile(filename,str):
        file = open(filename,'w')
        if file == None:
                return ""
        file.write(str)
        file.close()
    
    def sendMail(authInfo, fromAdd, toAdd, subject, plainText):
        strFrom = fromAdd
        strTo = toAdd
        server = authInfo.get('server')
        user = authInfo.get('user')
        passwd = authInfo.get('password')
        port = authInfo.get('port')
    
        if not (server and user and passwd) :
            return
    
        msgRoot = MIMEMultipart('related')
        msgRoot['Subject'] = subject
        msgRoot['From'] = strFrom
        msgRoot['To'] = strTo
        msgRoot['Date'] = formatdate(localtime=True)
    
        msgAlternative = MIMEMultipart('alternative')
        msgRoot.attach(msgAlternative)
    
        msgText = MIMEText(plainText,'plain','utf-8')
        msgAlternative.attach(msgText)
    
        smtp = smtplib.SMTP(server,port)
        smtp.ehlo()
        smtp.starttls()
        smtp.login(user,passwd)
        smtp.sendmail(strFrom, strTo, msgRoot.as_string())
        smtp.quit()
        return
    
    
    if __name__ == "__main__":
        httpurl = "http://city.ip138.com/ip2city.asp"
        strIP1 = getRemoteIp(httpurl)
        if strIP1 == None:
            print strIP1
            sys.exit()
        strIP2 = readFile("tmpip.dat")
        if strIP2 == None:
                sys.exit()
        writeFile("tmpip.dat",strIP1);
        strIP1 = "234.234.234.234"
        fromaddr = "xloogson@gmail.com"
        toaddr = "xloogson@qq.com"
        authInfo={}
        authInfo['server'] = "smtp.gmail.com"
        authInfo['user'] = "xloogson@gmail.com"
        authInfo['password'] = ""
        authInfo['port'] = 587
        if  strIP2 != "" and strIP1 != "" and strIP1 != strIP2 :
            subject = "check ip"
        	plainText = "ip change!!!" + "before=" + strIP1 + "now:" + strIP2;
        	sendMail(authInfo,fromaddr,toaddr,subject,plainText);
    

      

    参考链接:

      http://blog.csdn.net/JGood/article/details/4317416 http模块

      http://blog.donews.com/maverick/archive/2007/04/22/1159142.aspx smtp模块

      http://fayaa.com/code/view/11311/full/ smtp模块。

  • 相关阅读:
    分公司下拉框赋值-从后台传到前端jsp
    EASYUI DATAGRID加合计
    Quartz_TimeJob例子(C#)
    JAVA项目如何打开,打开乱码怎么办,字体太小怎么办,感叹号是什么情况
    下拉框设置下拉列表宽度
    获取dataset结果集的第一行第一列字段
    安装visio冲突
    ubuntu创建普通用户,解决远程登录失败
    ubuntu中文环境配置
    stackoverflow访问慢
  • 原文地址:https://www.cnblogs.com/xloogson/p/2478673.html
Copyright © 2011-2022 走看看