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


Python RequestHandler.set_cookie方法代码示例

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


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

示例1: track_page_view

# 需要导入模块: from tornado.web import RequestHandler [as 别名]
# 或者: from tornado.web.RequestHandler import set_cookie [as 别名]
def track_page_view(handler):
    """
    // Track a page view, updates all the cookies and campaign tracker,
    // makes a server side request to Google Analytics and writes the transparent
    // gif byte data to the response.
    """
    time_tup = time.localtime(time.time() + COOKIE_USER_PERSISTENCE)

    # set some useful items in environ:
    x_utmac = handler.request.arguments.get('x_utmac', '')

    domain = handler.request.headers.get('Host', '')

    # Get the referrer from the utmr parameter, this is the referrer to the
    # page that contains the tracking pixel, not the referrer for tracking
    # pixel.
    document_referer = handler.request.arguments.get('utmr', [])
    if not document_referer or document_referer == "0":
        document_referer = "-"
    else:
        document_referer = document_referer[0]
        document_referer = unquote(document_referer)

    document_path = handler.request.arguments.get('utmp', '')
    if document_path:
        document_path = document_path[0]
        document_path = unquote(document_path)

    account = handler.request.arguments.get('utmac', '')
    if account:
        account = account[0]

    user_agent = handler.request.headers.get('User-Agent', '')

    # // Try and get visitor cookie from the request.
    cookie = RequestHandler.get_cookie(handler, COOKIE_NAME)

    guidheader = handler.request.headers.get("X-DCMGUID", '')
    if not guidheader:
        guidheader = handler.request.headers.get("X-UP-SUBNO", '')
    if not guidheader:
        guidheader = handler.request.headers.get("X-JPHONE-UID", '')
    if not guidheader:
        guidheader = handler.request.headers.get("X-EM-UID", '')

    visitor_id = get_visitor_id(guidheader, account, user_agent, cookie)

    # // Always try and add the cookie to the response.
    # cookie = SimpleCookie()
    # cookie[COOKIE_NAME] = visitor_id
    # morsel = cookie[COOKIE_NAME]
    # morsel['expires'] = time.strftime('%a, %d-%b-%Y %H:%M:%S %Z', time_tup)
    # morsel['path'] = COOKIE_PATH
    expires = datetime(*time_tup[0:6])
    RequestHandler.set_cookie(handler, COOKIE_NAME, visitor_id, expires=expires)

    utm_gif_location = "http://www.google-analytics.com/__utm.gif"
    i = handler.request.headers.get("X-Forwarded-For", handler.request.headers.get("X-Real-Ip", None))
    if not i:
        i = handler.request.remote_ip
    i = i.split(",")[0]
    for utmac in [account, x_utmac]:
        if not utmac:
            continue
        # // Construct the gif hit url.
        utm_url = (utm_gif_location + "?" +
                "utmwv=" + VERSION +
                "&utmn=" + get_random_number() +
                "&utmhn=" + quote(domain) +
                "&utmsr=" + handler.request.arguments.get('utmsr', [''])[0] +
                "&utme=" + handler.request.arguments.get('utme', [''])[0] +
                "&utmr=" + quote(document_referer) +
                "&utmp=" + quote(document_path) +
                "&utmac=" + utmac +
                "&utmcc=__utma%3D999.999.999.999.999.1%3B" +
                "&utmvid=" + visitor_id +
                "&utmip=" + get_ip(i) +
                "&utmul=" + handler.request.headers.get("Accept-Language", '-') +
                "&utmcs=" + handler.request.headers.get("Accept-Charset", '-')
        )
        # dbgMsg("utm_url: " + utm_url)
        send_request_to_google_analytics(utm_url, handler)

    # // If the debug parameter is on, add a header to the response that contains
    # // the url that was used to contact Google Analytics.
    # headers = [('Set-Cookie', str(cookie).split(': ')[1])]
    headers = []
    if handler.request.arguments.get('utmdebug', False):
        headers.append(('X-GA-MOBILE-URL', utm_url))

    # Finally write the gif data to the response
    response = write_gif_data()
    response_headers = response['response_headers']
    response_headers.extend(headers)
    return response
开发者ID:dpnova,项目名称:tornado-mobile-google-analytics,代码行数:97,代码来源:tornadomobilega.py


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