import calendar
import datetime
now = datetime.datetime.now()
print calendar.monthrange(now.year, now.month)[1]
29
>>> from datetime import date
>>> (date(2012, 3, 1) - date(2012, 2, 1)).days
29
(dt.replace(month = dt.month % 12 +1, day = 1)-timedelta(days=1)).day
>>> from calendar import monthrange
>>> monthrange(2011, 2)
(1, 28)