Pengjun's profile上善若水PhotosBlogLists Tools Help

Blog


    September 25

    黄金分割

    Fibonacci 数列计算黄金分割 : 1, 1, 2, 3, 5,8,13,21 ... ...

    x = 2
    y = 3.0

    while y < 10000 :
    print x/y
    x ,y = y, x+y

    August 08

    取Gmail邮件(Draft)

    import getpass, poplib, base64
    import string ,email
    from BeautifulSoup import BeautifulSoup
    
    M = poplib.POP3_SSL('pop.gmail.com')
    M.user('jiapengjun')
    M.pass_(getpass.getpass())
    
    hdr,message,octet=M.retr(1000)
    mail=email.message_from_string(string.join(message,'\n'))
    
    print mail['To'] , mail['From']
    
    msg = ''
    for part in mail.get_payload() :
            type = part.get_content_charset()
            msg = part.get_payload()
    
    msg = unicode(base64.decodestring(msg), type)
    print msg
    
    #soup = BeautifulSoup(msg) 
    #print soup.prettify()
    #print soup.__str__('windows-1252')
    
    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()