python: September 2009 Archives

After reading Moving Planters Indoors for the Winter by Joseph Hall I decided to translate his script to Python.  Here's the result:

#!/usr/bin/python
import xml.etree.ElementTree as ET
import httplib
import smtplib
from email.mime.text import MIMEText

conn = httplib.HTTPConnection('www.wrh.noaa.gov')
conn.request('GET','/forecast/xml/xml.php?duration=168&interval=6&lat=40.69651&lon=-112.091784')
response = conn.getresponse()

forecast_xml = ET.parse(response)
low_temp = 100
for child in forecast_xml.getiterator():
    if child.tag == 'temperature':
        temp = int(child.text)
        low_temp = temp if temp < low_temp else low_temp

if low_temp < 40:
    msg = MIMEText('Temps as low as %s coming up' % low_temp)
    msg['Subject'] = 'Cold Weather'
    msg['From'] = from_addr = '[email protected]'
    msg['To'] = to_addr = '[email protected]'

    s = smtplib.SMTP('smtp.gmail.com', 587)
    s.starttls()
    s.login('username','password')
    s.sendmail(from_addr, to_addr, msg.as_string())
    s.quit()

About this Archive

This page is a archive of entries in the python category from September 2009.

Find recent content on the main index or look in the archives to find all content.

python: September 2009: Monthly Archives

Powered by Movable Type 5.04