login.py (to check password)
from urllib import urlopen, urlencode BASE_URL = 'http://d.hatena.ne.jp/' LOGIN_URL = BASE_URL + 'login' USERNAME = 'teamikl' PASSWORD = '********' param = urlencode({'key':USERNAME, 'password':PASSWORD}) urlobj = urlopen(LOGIN_URL, param) if urlobj.url == LOGIN_URL: print "failed" else: print "successed"
TODO - How to get cookie ? (urlopen does not set cookie?)
from urllib import urlencode import httplib BASE_URL = 'http://d.hatena.ne.jp/' LOGIN_URL = BASE_URL + 'login' def login(username, password): """ login(username, password) """ data = {'key':username, 'password':password} conn = httplib.HTTPConnection('d.hatena.ne.jp') conn.request('POST', '/login', urlencode(data)) res = conn.getresponse() location = res.getheader('location') if location == LOGIN_URL: raise HatenaLoginFailed elif location == BASE_URL + username: return res.getheader('Set-Cookie') else: raise HatenaError # XXX: I did not declare Exception classes, yet. # I want to use urllib, because I declare literals # LOGIN_URL, overwise I had hard coding the domain # and path, or I have to parse url string again. # urllib does it. But the problem, urllib does not # support cookie.