當前位置: 首頁>>代碼示例>>Python>>正文


Python Cookie.Morsel方法代碼示例

本文整理匯總了Python中Cookie.Morsel方法的典型用法代碼示例。如果您正苦於以下問題:Python Cookie.Morsel方法的具體用法?Python Cookie.Morsel怎麽用?Python Cookie.Morsel使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Cookie的用法示例。


在下文中一共展示了Cookie.Morsel方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: setcookie

# 需要導入模塊: import Cookie [as 別名]
# 或者: from Cookie import Morsel [as 別名]
def setcookie(name, value, expires='', domain=None,
              secure=False, httponly=False, path=None):
    """Sets a cookie."""
    morsel = Cookie.Morsel()
    name, value = safestr(name), safestr(value)
    morsel.set(name, value, urllib.quote(value))
    if expires < 0:
        expires = -1000000000
    morsel['expires'] = expires
    morsel['path'] = path or ctx.homepath+'/'
    if domain:
        morsel['domain'] = domain
    if secure:
        morsel['secure'] = secure
    value = morsel.OutputString()
    if httponly:
        value += '; httponly'
    header('Set-Cookie', value) 
開發者ID:joxeankoret,項目名稱:nightmare,代碼行數:20,代碼來源:webapi.py

示例2: cookies

# 需要導入模塊: import Cookie [as 別名]
# 或者: from Cookie import Morsel [as 別名]
def cookies(self):
        """A dictionary of Cookie.Morsel objects."""
        if not hasattr(self, "_cookies"):
            self._cookies = Cookie.SimpleCookie()
            if "Cookie" in self.headers:
                try:
                    parsed = parse_cookie(self.headers["Cookie"])
                except Exception:
                    pass
                else:
                    for k, v in parsed.items():
                        try:
                            self._cookies[k] = v
                        except Exception:
                            # SimpleCookie imposes some restrictions on keys;
                            # parse_cookie does not. Discard any cookies
                            # with disallowed keys.
                            pass
        return self._cookies 
開發者ID:tp4a,項目名稱:teleport,代碼行數:21,代碼來源:httputil.py

示例3: setcookie

# 需要導入模塊: import Cookie [as 別名]
# 或者: from Cookie import Morsel [as 別名]
def setcookie(self, key, value, max_age=None, expires=None, path='/', domain=None, secure=None, httponly=False):
        """
        Add a new cookie
        """
        newcookie = Morsel()
        newcookie.key = key
        newcookie.value = value
        newcookie.coded_value = value
        if max_age is not None:
            newcookie['max-age'] = max_age
        if expires is not None:
            newcookie['expires'] = expires
        if path is not None:
            newcookie['path'] = path
        if domain is not None:
            newcookie['domain'] = domain
        if secure:
            newcookie['secure'] = secure
        if httponly:
            newcookie['httponly'] = httponly
        self.sent_cookies = [c for c in self.sent_cookies if c.key != key]
        self.sent_cookies.append(newcookie) 
開發者ID:hubo1016,項目名稱:vlcp,代碼行數:24,代碼來源:http.py

示例4: destroy

# 需要導入模塊: import Cookie [as 別名]
# 或者: from Cookie import Morsel [as 別名]
def destroy(self, sessionid):
        """
        Destroy a session
        
        :param sessionid: session ID
        
        :return: a list of Set-Cookie headers to be sent to the client
        """
        await call_api(self.apiroutine, 'memorystorage', 'delete', {'key': __name__ + '.' + sessionid})
        m = Morsel()
        m.key = self.cookiename
        m.value = 'deleted'
        m.coded_value = 'deleted'
        opts = {'path':'/', 'httponly':True, 'max-age':0}
        m.update(opts)
        return [m] 
開發者ID:hubo1016,項目名稱:vlcp,代碼行數:18,代碼來源:session.py

示例5: setcookie

# 需要導入模塊: import Cookie [as 別名]
# 或者: from Cookie import Morsel [as 別名]
def setcookie(name, value, expires='', domain=None,
              secure=False, httponly=False, path=None):
    """Sets a cookie."""
    morsel = Morsel()
    name, value = safestr(name), safestr(value)
    morsel.set(name, value, quote(value))
    if isinstance(expires, int) and expires < 0:
        expires = -1000000000
    morsel['expires'] = expires
    morsel['path'] = path or ctx.homepath+'/'
    if domain:
        morsel['domain'] = domain
    if secure:
        morsel['secure'] = secure
    value = morsel.OutputString()
    if httponly:
        value += '; httponly'
    header('Set-Cookie', value) 
開發者ID:Naayouu,項目名稱:Hatkey,代碼行數:20,代碼來源:webapi.py

示例6: start

# 需要導入模塊: import Cookie [as 別名]
# 或者: from Cookie import Morsel [as 別名]
def start(self, cookies, cookieopts = None):
        """
        Session start operation. First check among the cookies to find existed sessions;
        if there is not an existed session, create a new one.
        
        :param cookies: cookie header from the client
        
        :param cookieopts: extra options used when creating a new cookie
        
        :return: ``(session_handle, cookies)`` where session_handle is a SessionHandle object,
                 and cookies is a list of created Set-Cookie headers (may be empty)
        """
        c = SimpleCookie(cookies)
        sid = c.get(self.cookiename)
        create = True
        if sid is not None:
            sh = await self.get(sid.value)
            if sh is not None:
                return (self.SessionHandle(sh, self.apiroutine), [])
        if create:
            sh = await self.create()
            m = Morsel()
            m.key = self.cookiename
            m.value = sh.id
            m.coded_value = sh.id
            opts = {'path':'/', 'httponly':True}
            if cookieopts:
                opts.update(cookieopts)
                if not cookieopts['httponly']:
                    del cookieopts['httponly']
            m.update(opts)
            return (sh, [m]) 
開發者ID:hubo1016,項目名稱:vlcp,代碼行數:34,代碼來源:session.py

示例7: __init__

# 需要導入模塊: import Cookie [as 別名]
# 或者: from Cookie import Morsel [as 別名]
def __init__(self, conpool=None, cookie_str=None, throw_exception=True,user_agent = None,headers=None):
        """conpool: 創建的連接池最大數量,類型為 int,默認為 10

            cookie_str: 用戶自己定義的 Cookie,類型為 String

            throw_exception: 是否拋出遇到的異常,類型為 bool,默認為 True

            為了全局配置useragent header 這裏加入相關選項
        """
        self.throw_exception = throw_exception
        if conpool is None:
            self.conpool = httpconpool(10)
        else:
            self.conpool = conpool
        Cookie.Morsel = MorselHook
        self.initcookie = Cookie.SimpleCookie()
        if cookie_str:
            if not cookie_str.endswith(';'):
                cookie_str += ";"
            for cookiepart in cookie_str.split(";"):
                if cookiepart.strip() != "":
                    cookiekey, cookievalue = cookiepart.split("=", 1)
                    self.initcookie[cookiekey.strip()] = cookievalue.strip()
        self.cookiepool = {}
        self.user_agent = user_agent
        self.headers = headers 
開發者ID:w-digital-scanner,項目名稱:w9scan,代碼行數:28,代碼來源:hackhttp.py

示例8: set_cookie

# 需要導入模塊: import Cookie [as 別名]
# 或者: from Cookie import Morsel [as 別名]
def set_cookie(self, name, value, domain=None, expires=None, path="/",
                   expires_days=None, **kwargs):
        """Sets the given cookie name/value with the given options.

        Additional keyword arguments are set on the Cookie.Morsel
        directly.
        See http://docs.python.org/library/cookie.html#morsel-objects
        for available attributes.
        """
        # The cookie library only accepts type str, in both python 2 and 3
        name = escape.native_str(name)
        value = escape.native_str(value)
        if re.search(r"[\x00-\x20]", name + value):
            # Don't let us accidentally inject bad stuff
            raise ValueError("Invalid cookie %r: %r" % (name, value))
        if not hasattr(self, "_new_cookie"):
            self._new_cookie = Cookie.SimpleCookie()
        if name in self._new_cookie:
            del self._new_cookie[name]
        self._new_cookie[name] = value
        morsel = self._new_cookie[name]
        if domain:
            morsel["domain"] = domain
        if expires_days is not None and not expires:
            expires = datetime.datetime.utcnow() + datetime.timedelta(
                days=expires_days)
        if expires:
            timestamp = calendar.timegm(expires.utctimetuple())
            morsel["expires"] = email.utils.formatdate(
                timestamp, localtime=False, usegmt=True)
        if path:
            morsel["path"] = path
        for k, v in kwargs.iteritems():
            if k == 'max_age':
                k = 'max-age'
            morsel[k] = v 
開發者ID:omererdem,項目名稱:honeything,代碼行數:38,代碼來源:web.py

示例9: cookies

# 需要導入模塊: import Cookie [as 別名]
# 或者: from Cookie import Morsel [as 別名]
def cookies(self):
        """A dictionary of Cookie.Morsel objects."""
        if not hasattr(self, "_cookies"):
            self._cookies = Cookie.SimpleCookie()
            if "Cookie" in self.headers:
                try:
                    self._cookies.load(
                        native_str(self.headers["Cookie"]))
                except Exception:
                    self._cookies = {}
        return self._cookies 
開發者ID:omererdem,項目名稱:honeything,代碼行數:13,代碼來源:httpserver.py

示例10: cookies

# 需要導入模塊: import Cookie [as 別名]
# 或者: from Cookie import Morsel [as 別名]
def cookies(self):
        """A dictionary of Cookie.Morsel objects."""
        if not hasattr(self, "_cookies"):
            self._cookies = Cookie.SimpleCookie()
            if "Cookie" in self.headers:
                try:
                    self._cookies.load(
                        native_str(self.headers["Cookie"]))
                except Exception:
                    self._cookies = None
        return self._cookies 
開發者ID:omererdem,項目名稱:honeything,代碼行數:13,代碼來源:wsgi.py

示例11: cookies

# 需要導入模塊: import Cookie [as 別名]
# 或者: from Cookie import Morsel [as 別名]
def cookies(self):
        """A dictionary of Cookie.Morsel objects."""
        if not hasattr(self, "_cookies"):
            self._cookies = Cookie.BaseCookie()
            if "Cookie" in self.request.headers:
                try:
                    self._cookies.load(self.request.headers["Cookie"])
                except:
                    self.clear_all_cookies()
        return self._cookies 
開發者ID:omererdem,項目名稱:honeything,代碼行數:12,代碼來源:web.py

示例12: set_cookie

# 需要導入模塊: import Cookie [as 別名]
# 或者: from Cookie import Morsel [as 別名]
def set_cookie(self, name, value, domain=None, expires=None, path="/",
                   expires_days=None, **kwargs):
        """Sets the given cookie name/value with the given options.

        Additional keyword arguments are set on the Cookie.Morsel
        directly.
        See http://docs.python.org/library/cookie.html#morsel-objects
        for available attributes.
        """
        name = _utf8(name)
        value = _utf8(value)
        if re.search(r"[\x00-\x20]", name + value):
            # Don't let us accidentally inject bad stuff
            raise ValueError("Invalid cookie %r: %r" % (name, value))
        if not hasattr(self, "_new_cookies"):
            self._new_cookies = []
        new_cookie = Cookie.BaseCookie()
        self._new_cookies.append(new_cookie)
        new_cookie[name] = value
        if domain:
            new_cookie[name]["domain"] = domain
        if expires_days is not None and not expires:
            expires = datetime.datetime.utcnow() + datetime.timedelta(
                days=expires_days)
        if expires:
            timestamp = calendar.timegm(expires.utctimetuple())
            new_cookie[name]["expires"] = email.utils.formatdate(
                timestamp, localtime=False, usegmt=True)
        if path:
            new_cookie[name]["path"] = path
        for k, v in kwargs.iteritems():
            new_cookie[name][k] = v 
開發者ID:omererdem,項目名稱:honeything,代碼行數:34,代碼來源:web.py


注:本文中的Cookie.Morsel方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。