opener.open(login_url)
try:
    token = [x.value for x in cookies.cookiejar if x.name == 'csrftoken'][0]
except IndexError:
    return False, 'no csrftoken'
params = dict(username=username, password=password, this_is_the_login_form=
    True, csrfmiddlewaretoken=token)
encoded_params = urllib.parse.urlencode(params)
with contextlib.closing(opener.open(login_url, encoded_params)) as f:
    html = f.read()
print(html)
How do I authenticate a urllib2 script in order to access HTTPS web services from a Django site?