Seeing as there is no really easy way to do a HTTP HEAD request from python, I wrote up the following small method:
In advance I’d like to apologize for the method that assemblies the request path.
Update: Added handling of redirects.
def http_head(url):
import httplib
import urlparse
redirects = 0
while redirects < 10:
scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
if scheme == 'https':
conn = httplib.HTTPSConnection(netloc)
else:
conn = httplib.HTTPConnection(netloc)
conn.request("HEAD", "%s%s%s%s%s" % (path, query and "?" or "", query,
fragment and "#" or "", fragment))
res = conn.getresponse()
if res.status in (301, 302) and res.getheader('location'):
url = res.getheader('location')
redirects += 1
else:
break
return res.status, res.reason
Posted by Mads Sülau Jørgensen at 12:30 pm on May 15th, 2009.
Categories: Uncategorized. Tags: HEAD, HTTP, Python, Work.
I keep forgetting how to format and indent xml from the command line. The tool xmllint does a fine job of doing just that, which has saved me numerous times whilst working with sports results. So. Much. Data.
Running
will re-format and re-indent the xml in the input file, and check it for various errors while doing it.
Posted by Mads Sülau Jørgensen at 3:11 pm on May 13th, 2009.
Categories: Uncategorized. Tags: Work, XML.
I needed to send an email, so I came up with this:
def send_plain_mail(subject, body, from_mail, to):
import smtplib
from email.MIMEText import MIMEText
from email.Encoders import encode_quopri
msg = MIMEText(body, 'plain', 'iso-8859-1')
msg['Subject'] = subject
msg['From'] = from_mail
msg['To'] = to
s = smtplib.SMTP()
s.connect()
s.sendmail(from_mail, [to], msg.as_string())
s.close()
Not rocket science, but it gets the job done.
Posted by Mads Sülau Jørgensen at 7:30 pm on May 11th, 2009.
Categories: Uncategorized. Tags: Email, Python, SMTP, Work.
Lets face it, I’m way to lazy to write blog posts at the moment, so I’m just going to publish some python code that i wrote today.
It’s a program that runs aacgain on all the albums in your iTunes library.
More… »
Posted by Mads Sülau Jørgensen at 8:34 pm on May 6th, 2009.
Categories: Uncategorized.