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


Python SimpleCookie.keys方法代码示例

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


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

示例1: _generate_proxy

# 需要导入模块: from six.moves.http_cookies import SimpleCookie [as 别名]
# 或者: from six.moves.http_cookies.SimpleCookie import keys [as 别名]
def _generate_proxy(current_path, value):
    where = value.split(':', 1)[1]
    cookie_name, routes = where.split(':', 1)
    routes = dict([ route.strip().split('=', 1) for route in routes.split(',') if route.strip() ])

    # cookie_name = 'weblabsessionid'
    # routes = {
    #    'route1' : 'http://localhost:10000/weblab/json/',
    #    'route2' : 'http://localhost:10001/weblab/json/',
    #    'route3' : 'http://localhost:10002/weblab/json/',
    # }

    current_cookie_value = request.cookies.get(cookie_name, '')

    chosen_url = None
    for route in routes:
        if current_cookie_value.endswith(route):
            chosen_url = routes[route]
            break

    if chosen_url is None:
        chosen_url = random.choice(list(routes.values()))

    headers = dict(request.headers)
    headers['X-Forwarded-For'] = request.remote_addr
    headers['X-Forwarded-Host'] = request.host
    headers.pop('Host', None)
    headers.pop('host', None)

    kwargs = dict(headers = headers, cookies = dict(request.cookies), allow_redirects=False)

    if request.method == 'GET':
        method = requests.get
    elif request.method == 'POST':
        kwargs['data'] = request.data

        if request.files:
            kwargs['files'] = {}
            for f, f_contents in six.iteritems(request.files):
                kwargs['files'][f] = [f_contents.filename, f_contents.stream, f_contents.content_type, f_contents.headers]

        if request.form:
            headers.pop('Content-Type', None)
            kwargs['data'] = request.form

        method = requests.post
    else:
        raise Exception("Method not supported")

    MAX_RETRIES = 5
    retry = 0
    full_url = chosen_url + current_path
    if request.args:
        full_url += '?' + '&'.join([ '%s=%s' % (key, requests.utils.quote(value, '')) for key, value in request.args.items() ])

    while True:
        try:
            req = method(full_url, **kwargs)
            break
        except requests.ConnectionError:
            if request.method != 'GET':
                raise
            retry += 1
            if retry >= MAX_RETRIES:
                raise
            time.sleep(0.5)

    cookies = list(req.cookies)
   
    headers = dict(req.headers)
    headers.pop('set-cookie', None)
    response_kwargs = {
        'headers' : headers,
        'status' : req.status_code,
    }
    if 'content-type' in req.headers:
        response_kwargs['content_type'] = req.headers['content-type']
    
    response = Response(req.content, **response_kwargs)
    
    existing_cookies = SimpleCookie()
    for header in response.headers:
        if header[0].lower() == 'set-cookie':
            try:
                if six.PY2:
                    cookie_header = header[1].encode('utf8')
                else:
                    cookie_header = header[1]
                existing_cookies.load(cookie_header)
            except Exception as e:
                print("Error processing cookie header: {}".format(cookie_header))
                import traceback
                traceback.print_exc()

    for c in req.cookies:
        if c.name not in existing_cookies.keys():
            response.set_cookie(c.name, c.value, path=c.path, expires=c.expires, secure=c.secure)

    return response
开发者ID:labsland,项目名称:weblabdeusto,代码行数:101,代码来源:proxy_server.py


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