Websites by Email

I'd just like to share with you these forty-seven lines of python code that amaze me. Not because they do anything special, but because they represent something that is pretty important to programming languages, modules, or libraries (depending on the language). Let's take a quick gander at the code:

  1. #!/usr/bin/python
  2. """
  3. #
  4. # This is a simple script to grab the contents of a a website,
  5. # encoded into a MIME message, and email it
  6. """
  7. import sys
  8. import smtplib
  9. import pycurl
  10. from cStringIO import StringIO
  11. from email.mime.text import MIMEText
  12. from email.mime.multipart import MIMEMultipart
  14. data_buf = StringIO()
  15. curl = pycurl.Curl()
  17. # Setup pycurl to grab the data
  18. curl.setopt(pycurl.URL, sys.argv[1])
  19. curl.setopt(pycurl.WRITEFUNCTION, data_buf.write )
  20. curl.perform()
  21. curl.close()
  23. # Begin creating email
  24. #create html & text parts of the email
  25. part1 = MIMEText(data_buf.getvalue(), 'html')
  26. part2 = MIMEText(data_buf.getvalue(), 'text')
  28. # next 5 lines put it all together
  29. msg = MIMEMultipart('alternative')
  30. msg['Subject'] = 'Website by Email'
  32. msg.attach(part1)
  33. msg.attach(part2)
  35. # email away
  36. # this code was copied from:
  37. #
  38. # Geshi keeps adding extra lines here, I'm not sure why
  39. to = 'add_your_own'
  40. gmail_user = 'add_your_own'
  41. gmail_pwd = 'add_your_own'
  42. smtpserver = smtplib.SMTP("", 587)
  43. smtpserver.ehlo()
  44. smtpserver.starttls()
  45. smtpserver.login(gmail_user, gmail_pwd)
  46. smtpserver.sendmail(gmail_user, to, msg.as_string())
  47. print 'done!'
  48. smtpserver.quit()

This little script started out as a work assignment (which I have modified to make it more general). The task was to grab the contents of a particular website and email them. Not terribly complicated, but also not one I was thinking would be accomplished with twenty-eight lines of actual code.

Let's take a second to review which modules are being used above:

The code used by PycURL to grab the website, and deposit the data into the string.

StringIO module, to hold the contents of the website. (I wasn't able to get a regular string to work here. If you know how, please tell me.)

The MIME module for reformatting the website contents.

The SMTPlib module, for having all the code in it to properly communicate with a SMTP server, including TLS for encryption, to send said email through the web.

It wasn't until doing this project that I fully realized the importance of modules. It was these four modules that saved me uncountable hours of coding, testing, and debugging. Even if I had written out the functionality I needed from scratch, what I would have written would not have anywhere near the functionality that these other modules provided. In correlation to this, I wonder if there is some kind of connection between how popular a lanugage is and how easily extendable it is. I have no way to prove this of course, but both Perl & Python could be good examples (and also happen to be the languages I'm most familiar with). Both languages are popular, as shown by the normalized graph by, and both are also extremely easy to add extra functionality to. The PycURL, CLyther, and PyCUDA modules are great cases in point. PycURL allows Python to tap into the CURL library. CLyther allows Python to use OpenCL, and PyCUDA allows Python to access the CUDA libraries. It makes my head hurt just to think about the amount of code that it would take to perform these same functions, if written from scratch.

After having this moment of realization, I find I am extremely grateful to all the programmers out there who put in their time to help create modules like these and to help them perform as well as they do. I tip my hat at you all.

If you made it this far down into the article, hopefully you liked it enough to share it with your friends. Thanks if you do, I appreciate it.

Bookmark and Share

Syndicate content