Code example for logging on to Tom via python and pythontom via email
In many python tutorials, we will talk about examples of logging on to or sending emails. This article also provides an example. Log on to tom's mailbox and send a mail. The Code is as follows:
- Def loginTom (username, password ):
- Url1 = '''
- Http://login.mail.tom.com/cgi/login
- '''
- Values = {
- 'Type': '0 ',
- 'User': '% s' % username,
- 'In _ username': '% s@tom.com' % username,
- 'Pass': '% s' % password,
- 'Style': '21 ',
- 'Verifycooker': 'y'
- }
- Data = urllib. urlencode (values)
- Req = urllib2.Request (url1, data)
- Response = opener. open (req)
- Data2 = response. read ()
- Sid = re. search (R '(? <= (Sid = )).*? (? = &) ', Data2). group ()
- Url3 = '''
- Http://bjapp6.mail.tom.com/cgi/ldapapp? Funcid = mails & sid = % s & fid = 1
- ''' % Sid
- Response = opener. open (url3)
- Data3 = response. read ()
- B = re. search (R '(? <= NTotalMailCount ).*? (? =;) ', Data3). group ()
- C = re. search (R' \ d. + ', B). group ()
- Num_per_page = 20
- Num_times = string. atoi (c)/20
- Print (num_times)
- Index = 0
- For match in re. finditer (R '(? <= "Mbox_Td_Subject" \> ).*? (? =\</) ', Data3 ):
- Index + = 1
- Part1 = "% d mail" % index
- Part2 = "% s" % match. group ()
- Part1 = part1.decode ('utf8'). encode ('gbk ')
- Subject = re. search (R '(? <=>). + ', Part2). group ()
- Subject = part1.decode ('gbk') + "" + subject. decode ('gbk ')
- Print (subject. encode ('gbk '))
- For I in xrange (num_times-1 ):
- Url3 = '''
- Http://bjapp6.mail.tom.com/cgi/ldapapp? Funcid = mails & sid = % s & fid = 1 & start = % d
- ''' % (Sid, (I + 1) * num_per_page)
- Response = opener. open (url3)
- Data3 = response. read ()
- For match in re. finditer (R '(? <= "Mbox_Td_Subject" \> ).*? (? =\</) ', Data3 ):
- Index + = 1
- Part1 = "% d mail" % index
- Part2 = "% s" % match. group ()
- Part1 = part1.decode ('utf8'). encode ('gbk ')
- Subject = re. search (R '(? <=>). + ', Part2). group ()
- Subject = part1.decode ('gbk') + "" + subject. decode ('gbk ')
- Print (subject. encode ('gbk '))