more fun with Python
14 March 2001 06:19 pmI started fiddling around with some python in the general direction of an LJ client while trying to stay off the phone the last hour.
I was waiting for a phone interview call which has not yet come. The nice, helpful Catbert (fourth) managed to determine that the fellow had got caught up in a meeting, which he admitted happens from time to time...I am to catch up with them tomorrow morning.
I had looked at the dev pages to see what I would need, (and they say it better ) so I knew I really just needed some simple web client code (HTTP GET, PUT) at the base of it. From wandering around in the Python Library Reference before, I knew it was really easy to get into any common internet protocols (Awhile back I hacked up a 7 line email client to see if I could..) so I started poking around their url and http libraries (urllib and httplib, respectively :) This is what I came up with in less than an hour:
def fetch0(bone, meat):
"""v0 Fetches file string meat from host bone by http"""
import httplib
h1 = httplib.HTTP(host=bone)
h1.putrequest('GET', meat)
h1.putheader('ACCEPT', 'text/html')
h1.putheader('ACCEPT', 'text/plain')
h1.endheaders()
errcode, errmsg, headers = h1.getreply()
if errcode == 200:
f = h1.getfile()
data = f.read()
f.close()
print data
else:
print errcode, errmsg
Drop this into a python interpreter or type it in, feed in a string pair, like so:
fetch0('www.livejournal.com','/~adric/')
and it will merrily dump the HTML of the file to screen, or report the error code.
So simple it hurts.