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模块。

  • 相关阅读:
    P1032 字串变换
    P3203 [HNOI2010]弹飞绵羊
    P3690 【模板】Link Cut Tree (动态树)
    P2147 [SDOI2008]洞穴勘测
    P3950 部落冲突
    Codeforces Round #469 Div. 2题解
    线段树
    SDOI2018退役记
    4.1模拟题
    无旋Treap
  • 原文地址:https://www.cnblogs.com/xloogson/p/2478673.html
Copyright © 2011-2022 走看看