Code example for logging on to Tom via python and pythontom via email

Source: Internet
Author: User

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:

  1. Def loginTom (username, password ):
  2. Url1 = '''
  3. Http://login.mail.tom.com/cgi/login
  4. '''
  5. Values = {
  6. 'Type': '0 ',
  7. 'User': '% s' % username,
  8. 'In _ username': '% s@tom.com' % username,
  9. 'Pass': '% s' % password,
  10. 'Style': '21 ',
  11. 'Verifycooker': 'y'
  12. }
  13. Data = urllib. urlencode (values)
  14. Req = urllib2.Request (url1, data)
  15. Response = opener. open (req)
  16. Data2 = response. read ()
  17. Sid = re. search (R '(? <= (Sid = )).*? (? = &) ', Data2). group ()
  18. Url3 = '''
  19. Http://bjapp6.mail.tom.com/cgi/ldapapp? Funcid = mails & sid = % s & fid = 1
  20. ''' % Sid
  21. Response = opener. open (url3)
  22. Data3 = response. read ()
  23. B = re. search (R '(? <= NTotalMailCount ).*? (? =;) ', Data3). group ()
  24. C = re. search (R' \ d. + ', B). group ()
  25. Num_per_page = 20
  26. Num_times = string. atoi (c)/20
  27. Print (num_times)
  28. Index = 0
  29. For match in re. finditer (R '(? <= "Mbox_Td_Subject" \> ).*? (? =\</) ', Data3 ):
  30. Index + = 1
  31. Part1 = "% d mail" % index
  32. Part2 = "% s" % match. group ()
  33. Part1 = part1.decode ('utf8'). encode ('gbk ')
  34. Subject = re. search (R '(? <=>). + ', Part2). group ()
  35. Subject = part1.decode ('gbk') + "" + subject. decode ('gbk ')
  36. Print (subject. encode ('gbk '))
  37. For I in xrange (num_times-1 ):
  38. Url3 = '''
  39. Http://bjapp6.mail.tom.com/cgi/ldapapp? Funcid = mails & sid = % s & fid = 1 & start = % d
  40. ''' % (Sid, (I + 1) * num_per_page)
  41. Response = opener. open (url3)
  42. Data3 = response. read ()
  43. For match in re. finditer (R '(? <= "Mbox_Td_Subject" \> ).*? (? =\</) ', Data3 ):
  44. Index + = 1
  45. Part1 = "% d mail" % index
  46. Part2 = "% s" % match. group ()
  47. Part1 = part1.decode ('utf8'). encode ('gbk ')
  48. Subject = re. search (R '(? <=>). + ', Part2). group ()
  49. Subject = part1.decode ('gbk') + "" + subject. decode ('gbk ')
  50. Print (subject. encode ('gbk '))
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.