Pengjun's profile上善若水PhotosBlogLists Tools Help

Blog


    May 12

    使用smtplib 发送email.

    # Import smtplib for the actual sending function
    import smtplib
    
    # Import the email modules we'll need
    from email.MIMEText import MIMEText
    me = 'pjia@gmail.com'
    you = 'pjia@gmail.com'
    textfile = 'D:/workspace/stax.dtd'
    
    # Open a plain text file for reading.  For this example, assume that
    # the text file contains only ASCII characters.
    fp = open(textfile, 'rb')
    # Create a text/plain message
    msg = MIMEText(fp.read())
    fp.close()
    
    # me == the sender's email address
    # you == the recipient's email address
    msg['Subject'] = 'The contents of %s' % textfile
    msg['From'] = me
    msg['To'] = you
    
    # Send the message via our own SMTP server, but don't include the
    # envelope header.
    s = smtplib.SMTP()
    s.connect('bjlinux12:25')
    s.sendmail(me, [you], msg.as_string())
    s.close()
    
    

    使用telnetlib 登陆telnet.

    import getpass
    import sys
    import telnetlib
    import string
    import time
    
    HOST = "bjhp8"
    user = raw_input("Enter your remote account: ")
    password = getpass.getpass()
    
    tn = telnetlib.Telnet(HOST)
    
    # 显示DEBUG信息,打开下边的注释
    # tn.set_debuglevel(10)  
    
    tn.read_until("login:")
    tn.write(user + "\n")           # 如果不work, 试试使用'\r\n'
    if password:
        tn.read_until("Password: ")
        tn.write(password + "\n")
    
    # 有些系统需要先读取缓冲后才可以发送命令
    m_msg = tn.read_until('>', 5)
    print m_msg
    
    # 倒霉的SUSE还要设置一下TERM type.
    if string.find(m_msg , 'tset') != -1 :
            tn.write("vt100\n")
    
    tn.write("ls\n")
    print tn.read_until('>', 5)
    tn.write("exit\n")
    
    # 对方断开链接后,使用read_all读取返回信息
    print tn.read_all()