#!/usr/bin/python
# -*- encoding: utf-8 -*-

##
## url_get_with_auth.py
## Created on Fri Dec 22 15:50:00 2006
## by Antti Kaihola <akaihola@ambitone.com>
## 
## Copyright (C) 2006 Antti Kaihola
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##

import urllib2, cookielib

base_url = 'http://trac.ambitone.com/ambidjangolib'
login_url = '%s/login' % base_url
report_url = '%s/report/1?format=csv' % base_url
username = raw_input('Username: ')
password = raw_input('Password: ')
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, 'http://trac.ambitone.com/ambidjangolib', username, password)
auth_handler = urllib2.HTTPDigestAuthHandler(passman)
jar = cookielib.CookieJar()
cookie_processor = urllib2.HTTPCookieProcessor(jar)
opener = urllib2.build_opener(cookie_processor, auth_handler)
urllib2.install_opener(opener)
urllib2.urlopen(login_url)
print urllib2.urlopen(report_url).read()
