当前位置: 首页>>代码示例>>Python>>正文


Python SimpleCookie.clear方法代码示例

本文整理汇总了Python中Cookie.SimpleCookie.clear方法的典型用法代码示例。如果您正苦于以下问题:Python SimpleCookie.clear方法的具体用法?Python SimpleCookie.clear怎么用?Python SimpleCookie.clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Cookie.SimpleCookie的用法示例。


在下文中一共展示了SimpleCookie.clear方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Session

# 需要导入模块: from Cookie import SimpleCookie [as 别名]
# 或者: from Cookie.SimpleCookie import clear [as 别名]
class Session(object):
    def __init__(self, name='sid', dir=path_join(WORK_DIR, 'sessions'),
            path=None, domain=None, max_age=None):

        self._name = name
        now = datetime.utcnow();

        # blank cookie
        self._cookie = SimpleCookie()

        if environ.has_key('HTTP_COOKIE'):
            # cookie already exists, see what's in it
            self._cookie.load(environ['HTTP_COOKIE'])

        try:
            # what's our session ID?
            self.sid = self._cookie[name].value;
        except KeyError:
            # there isn't any, make a new session ID
            remote = environ.get('REMOTE_ADDR')
            self.sid = sha224('%s-%s' % (remote, now)).hexdigest()

        self._cookie.clear();
        self._cookie[name] = self.sid

        # set/reset path
        if path:
            self._cookie[name]['path'] = path
        else:
            self._cookie[name]['path'] = ''

        # set/reset domain
        if domain:
            self._cookie[name]['domain'] = domain
        else:
            self._cookie[name]['domain'] = ''

        # set/reset expiration date
        if max_age:
            if isinstance(max_age, int):
                max_age = timedelta(seconds=max_age)
            expires = now + max_age
            self._cookie[name]['expires'] = expires.strftime('%a, %d %b %Y %H:%M:%S')
        else:
            self._cookie[name]['expires'] = ''

        # to protect against cookie-stealing JS, make our cookie
        # available only to the browser, and not to any scripts
        try:
            # This will not work for Python 2.5 and older
            self._cookie[name]['httponly'] = True
        except CookieError:
            pass

        # if the sessions dir doesn't exist, create it
        if not exists(dir):
            mkdir(dir)
        # persist the session data
        self._shelf_file = path_join(dir, self.sid)
        # -1 signifies the highest available protocol version
        self._shelf = shelve_open(self._shelf_file, protocol=-1, writeback=True)

    def print_cookie(self):
        # send the headers
        print "Cache-Control: no-store, no-cache, must-revalidate"
        print self._cookie

    def close(self):
        # save the data
        self._shelf.close()

    def invalidate(self):
        from os import unlink

        # remove and expire the session
        self._shelf.close()
        unlink(self._shelf_file)
        self._cookie[self._name]['expires'] = 0

    def __getitem__(self, key):
        return self._shelf[key]

    def __setitem__(self, key, value):
        self._shelf[key] = value

    def __delitem__(self, key):
        del self._shelf[key]

    def get(self, key, default=None):
        # FIXME: for some reason, doesn't work:
        # self._shelf.get(key, default)
        #
        # instead:
        try:
            return self._shelf[key]
        except KeyError:
            return default
开发者ID:dmcc,项目名称:brat,代码行数:99,代码来源:session.py


注:本文中的Cookie.SimpleCookie.clear方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。