<filename>www/util/handlerutil.py
from _datetime import datetime, date
import asyncio
import hashlib
import json
import logging
import time
from www.models import User

__author__ = 'sunshine'


def user2cookie(user, max_age, cookie_key):
    """
    计算加密cookie
    :param user:
    :param max_age:
    :param cookie_key:
    :return:
    """
    # build cookie string by: id-expires-sha1
    expires = str(int(time.time() + max_age))
    s = '%s-%s-%s-%s' % (user.id, user.password, expires, cookie_key)
    l = [user.id, expires, hashlib.sha1(s.encode('utf-8')).hexdigest()]
    return '-'.join(l)


@asyncio.coroutine
def cookie2user(cookie_str, cookie_key):
    if not cookie_str:
        return None
    try:
        L = cookie_str.split('-')
        if len(L) != 3:
            return None
        uid, expires, sha1 = L
        if int(expires) < time.time():
            return None
        user = yield from User.find(uid)
        if user is None:
            return None
        s = '%s-%s-%s-%s' % (uid, user.password, expires, cookie_key)
        if sha1 != hashlib.sha1(s.encode('utf-8')).hexdigest():
            logging.info('invalid sha1')
            return None
        user.passwd = '******'
        return user
    except Exception as e:
        logging.exception(e)
        return None



class JSONEncoder(json.JSONEncoder):
    """
    将时间类型的转为字符串json.dumps
    """
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, obj)
<filename>www/util/handlerutil.py
from _datetime import datetime, date
import asyncio
import hashlib
import json
import logging
import time
from www.models import User

__author__ = 'sunshine'


def user2cookie(user, max_age, cookie_key):
    """
    计算加密cookie
    :param user:
    :param max_age:
    :param cookie_key:
    :return:
    """
    # build cookie string by: id-expires-sha1
    expires = str(int(time.time() + max_age))
    s = '%s-%s-%s-%s' % (user.id, user.password, expires, cookie_key)
    l = [user.id, expires, hashlib.sha1(s.encode('utf-8')).hexdigest()]
    return '-'.join(l)


@asyncio.coroutine
def cookie2user(cookie_str, cookie_key):
    if not cookie_str:
        return None
    try:
        L = cookie_str.split('-')
        if len(L) != 3:
            return None
        uid, expires, sha1 = L
        if int(expires) < time.time():
            return None
        user = yield from User.find(uid)
        if user is None:
            return None
        s = '%s-%s-%s-%s' % (uid, user.password, expires, cookie_key)
        if sha1 != hashlib.sha1(s.encode('utf-8')).hexdigest():
            logging.info('invalid sha1')
            return None
        user.passwd = '******'
        return user
    except Exception as e:
        logging.exception(e)
        return None



class JSONEncoder(json.JSONEncoder):
    """
    将时间类型的转为字符串json.dumps
    """
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, obj)
计算加密cookie     :param user:     :param max_age:     :param cookie_key:     :return: # build cookie string by: id-expires-sha1 将时间类型的转为字符串json.dumps