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


Python StreamingHttpResponse.set_cookie方法代码示例

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


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

示例1: comments

# 需要导入模块: from django.http import StreamingHttpResponse [as 别名]
# 或者: from django.http.StreamingHttpResponse import set_cookie [as 别名]
def comments(request, page=1):
    perPage = 20
    slice_start = perPage*int(page)-perPage
    slice_end = perPage*int(page)

    comments = Comments.objects.filter(is_removed=False).order_by('-posted')
    amount = len(comments)
    rowsRange = int(math.ceil(amount/float(perPage)))   # amount of rows
    comments = comments[slice_start:slice_end]
    amount_this_page = len(comments)

    if amount_this_page == 0 and int(page) != 1:
        return HttpResponseRedirect("/comments/")

    last_comment_id_seen = request.COOKIES.get('last_comment_id_seen', comments[0].id)

    template = loader.get_template('index.html')
    template_args = {
        'content': 'comments.html',
        'request': request,
        'title': ' - Comments',
        'comments': comments,
        'amount': amount,
        'amount_this_page': amount_this_page,
        'range': [i+1 for i in range(rowsRange)],
        'page': int(page),
        'last_comment_id_seen': int(last_comment_id_seen),
    }
    response = StreamingHttpResponse(template.render(template_args, request))
    if int(page) == 1:
        response.set_cookie('last_comment_id_seen', comments[0].id, max_age=4320000)
    return response
开发者ID:ihptru,项目名称:OpenRA-Resources,代码行数:34,代码来源:views.py

示例2: generate_response

# 需要导入模块: from django.http import StreamingHttpResponse [as 别名]
# 或者: from django.http.StreamingHttpResponse import set_cookie [as 别名]
def generate_response(filepath, content_type, filename=None):
    filename = filename or os.path.basename(filepath)
    response = StreamingHttpResponse(export_iterator(filepath), content_type=content_type)
    response['Content-Length'] = default_storage.size(filepath)
    response['Content-Disposition'] = "attachment; filename=%s" % filename
    response.set_cookie(key='fileDownload', value="true")
    response.set_cookie(key='path', value="/")
    return response
开发者ID:fle-internal,项目名称:content-curation,代码行数:10,代码来源:channels.py

示例3: get_django_response

# 需要导入模块: from django.http import StreamingHttpResponse [as 别名]
# 或者: from django.http.StreamingHttpResponse import set_cookie [as 别名]
def get_django_response(proxy_response):
    """This method is used to create an appropriate response based on the
    Content-Length of the proxy_response. If the content is bigger than
    MIN_STREAMING_LENGTH, which is found on utils.py,
    than django.http.StreamingHttpResponse will be created,
    else a django.http.HTTPResponse will be created instead

    :param proxy_response: An Instance of urllib3.response.HTTPResponse that
                           will create an appropriate response
    :returns: Returns an appropriate response based on the proxy_response
              content-length
    """
    status = proxy_response.status
    headers = proxy_response.headers

    logger.debug('Proxy response headers: %s', headers)

    content_type = headers.get('Content-Type')

    logger.debug('Content-Type: %s', content_type)

    if should_stream(proxy_response):
        logger.info('Content-Length is bigger than %s', DEFAULT_AMT)
        response = StreamingHttpResponse(proxy_response.stream(DEFAULT_AMT),
                                         status=status,
                                         content_type=content_type)
    else:
        content = proxy_response.data or b''
        response = HttpResponse(content, status=status,
                                content_type=content_type)

    logger.info("Normalizing headers that aren't in IGNORE_HEADERS")
    for header, value in headers.items():
        if header.lower() not in IGNORE_HEADERS:
            response[header.title()] = value

    logger.debug('Response headers: %s', getattr(response, '_headers'))

    cookies = proxy_response.headers.getlist('set-cookie')
    logger.info('Checking for invalid cookies')
    for cookie_string in cookies:
        cookie_dict = cookie_from_string(cookie_string)
        # if cookie is invalid cookie_dict will be None
        if cookie_dict:
            response.set_cookie(**cookie_dict)

    logger.debug('Response cookies: %s', response.cookies)

    return response
开发者ID:interlegis,项目名称:django-revproxy,代码行数:51,代码来源:response.py

示例4: get_django_response

# 需要导入模块: from django.http import StreamingHttpResponse [as 别名]
# 或者: from django.http.StreamingHttpResponse import set_cookie [as 别名]
def get_django_response(proxy_response, strict_cookies=False):
    """This method is used to create an appropriate response based on the
    Content-Length of the proxy_response. If the content is bigger than
    MIN_STREAMING_LENGTH, which is found on utils.py,
    than django.http.StreamingHttpResponse will be created,
    else a django.http.HTTPResponse will be created instead

    :param proxy_response: An Instance of urllib3.response.HTTPResponse that
                           will create an appropriate response
    :param strict_cookies: Whether to only accept RFC-compliant cookies
    :returns: Returns an appropriate response based on the proxy_response
              content-length
    """
    status = proxy_response.status
    headers = proxy_response.headers

    logger.debug('Proxy response headers: %s', headers)

    content_type = headers.get('Content-Type')

    logger.debug('Content-Type: %s', content_type)

    if should_stream(proxy_response):
        logger.info('Content-Length is bigger than %s', DEFAULT_AMT)
        response = StreamingHttpResponse(proxy_response.stream(DEFAULT_AMT),
                                         status=status,
                                         content_type=content_type)
    else:
        content = proxy_response.data or b''
        response = HttpResponse(content, status=status,
                                content_type=content_type)

    logger.info('Normalizing response headers')
    set_response_headers(response, headers)

    logger.debug('Response headers: %s', getattr(response, '_headers'))

    cookies = proxy_response.headers.getlist('set-cookie')
    logger.info('Checking for invalid cookies')
    for cookie_string in cookies:
        cookie_dict = cookie_from_string(cookie_string,
                                         strict_cookies=strict_cookies)
        # if cookie is invalid cookie_dict will be None
        if cookie_dict:
            response.set_cookie(**cookie_dict)

    logger.debug('Response cookies: %s', response.cookies)

    return response
开发者ID:TracyWebTech,项目名称:django-revproxy,代码行数:51,代码来源:response.py

示例5: do_request

# 需要导入模块: from django.http import StreamingHttpResponse [as 别名]
# 或者: from django.http.StreamingHttpResponse import set_cookie [as 别名]
    def do_request(self, request, url, method, request_data):

        url = iri_to_uri(url)

        request_data.update({
            "method": method,
            "url": url,
            "original-request": request,
        })

        request_data.setdefault("data", None)
        request_data.setdefault("headers", {})
        request_data.setdefault("cookies", SimpleCookie())
        request_data.setdefault("user", request.user)

        # Request creation
        proto, host, cgi, param, query = urlparse(url)[:5]

        # Build the Via header
        protocolVersion = self.protocolRE.match(request.META['SERVER_PROTOCOL'])
        if protocolVersion is not None:
            protocolVersion = protocolVersion.group(1)
        else:
            protocolVersion = '1.1'

        via_header = "%s %s (Wirecloud-python-Proxy/1.1)" % (protocolVersion, get_current_domain(request))
        if 'via' in request_data['headers']:
            request_data['headers']['via'] += ', ' + via_header
        else:
            request_data['headers']['via'] = via_header

        # XFF headers
        if 'x-forwarded-for' in request_data['headers']:
            request_data['headers']['x-forwarded-for'] += ', ' + request.META['REMOTE_ADDR']
        else:
            request_data['headers']['x-forwarded-for'] = request.META['REMOTE_ADDR']

        # Pass proxy processors to the new request
        try:
            for processor in get_request_proxy_processors():
                processor.process_request(request_data)
        except ValidationError as e:
            return e.get_response(request)

        # Cookies
        cookie_header_content = ', '.join([request_data['cookies'][key].OutputString() for key in request_data['cookies']])
        if cookie_header_content != '':
            request_data['headers']['Cookie'] = cookie_header_content

        # Seems that Django or WSGI provides default values for the
        # Content-Length and Content-Type headers, so we are not able to detect
        # if the request provided them :(
        if str(request_data['headers'].get('content-length', '0')).strip() == '0':
            request_data['data'] = None
            if 'content-type' in request_data['headers']:
                del request_data['headers']['content-type']

        # Open the request
        try:
            res = requests.request(request_data['method'], request_data['url'], headers=request_data['headers'], data=request_data['data'], stream=True, verify=getattr(settings, 'WIRECLOUD_HTTPS_VERIFY', True))
        except requests.exceptions.Timeout as e:
            return build_error_response(request, 504, _('Gateway Timeout'), details=six.text_type(e))
        except requests.exceptions.SSLError as e:
            return build_error_response(request, 502, _('SSL Error'), details=six.text_type(e))
        except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError, requests.exceptions.TooManyRedirects) as e:
            return build_error_response(request, 504, _('Connection Error'), details=six.text_type(e))

        # Build a Django response
        response = StreamingHttpResponse(res.raw.stream(4096, decode_content=False), status=res.status_code, reason=res.reason)

        # Add all the headers received from the response
        for header in res.headers:

            header_lower = header.lower()
            if header_lower == 'set-cookie':

                for cookie in res.cookies:
                    response.set_cookie(cookie.name, value=cookie.value, expires=cookie.expires, path=cookie.path)

            elif header_lower == 'via':

                via_header = via_header + ', ' + res.headers[header]

            elif is_valid_response_header(header_lower):
                response[header] = res.headers[header]

        # Pass proxy processors to the response
        for processor in get_response_proxy_processors():
            response = processor.process_response(request_data, response)

        response['Via'] = via_header

        return response
开发者ID:Wirecloud,项目名称:wirecloud,代码行数:95,代码来源:views.py

示例6: do_request

# 需要导入模块: from django.http import StreamingHttpResponse [as 别名]
# 或者: from django.http.StreamingHttpResponse import set_cookie [as 别名]

#.........这里部分代码省略.........
            if header_name == 'content_type' and header[1]:
                request_data['headers']["content-type"] = header[1]

            elif header_name == 'content_length' and header[1]:
                # Only take into account request body if the request has a
                # Content-Length header (we don't support chunked requests)
                request_data['data'] = request
                request_data['headers']['content-length'] = header[1]
                request_data['data'].len = int(header[1])

            elif header_name == 'cookie' or header_name == 'http_cookie':

                cookie_parser = SimpleCookie(str(header[1]))

                del cookie_parser[str(settings.SESSION_COOKIE_NAME)]

                if str(settings.CSRF_COOKIE_NAME) in cookie_parser:
                    del cookie_parser[str(settings.CSRF_COOKIE_NAME)]

                request_data['cookies'].update(cookie_parser)

            elif self.http_headerRE.match(header_name) and not header_name in self.blacklisted_http_headers:

                fixed_name = header_name.replace("http_", "", 1).replace('_', '-')
                request_data['headers'][fixed_name] = header[1]

        # Build the Via header
        protocolVersion = self.protocolRE.match(request.META['SERVER_PROTOCOL'])
        if protocolVersion is not None:
            protocolVersion = protocolVersion.group(1)
        else:
            protocolVersion = '1.1'

        via_header = "%s %s (Wirecloud-python-Proxy/1.1)" % (protocolVersion, get_current_domain(request))
        if 'via' in request_data['headers']:
            request_data['headers']['via'] += ', ' + via_header
        else:
            request_data['headers']['via'] = via_header

        # XFF headers
        if 'x-forwarded-for' in request_data['headers']:
            request_data['headers']['x-forwarded-for'] += ', ' + request.META['REMOTE_ADDR']
        else:
            request_data['headers']['x-forwarded-for'] = request.META['REMOTE_ADDR']

        request_data['headers']['x-forwarded-host'] = host
        if 'x-forwarded-server' in request_data['headers']:
            del request_data['headers']['x-forwarded-server']

        # Pass proxy processors to the new request
        try:
            for processor in get_request_proxy_processors():
                processor.process_request(request_data)
        except ValidationError as e:
            return e.get_response(request)

        # Cookies
        cookie_header_content = ', '.join([cookie_parser[key].OutputString() for key in request_data['cookies']])
        if cookie_header_content != '':
            request_data['headers']['Cookie'] = cookie_header_content

        # Open the request
        try:
            res = requests.request(request_data['method'], request_data['url'], headers=request_data['headers'], data=request_data['data'], stream=True, verify=getattr(settings, 'WIRECLOUD_HTTPS_VERIFY', True))
        except requests.exceptions.Timeout as e:
            return build_error_response(request, 504, _('Gateway Timeout'), details=six.text_type(e))
        except requests.exceptions.SSLError as e:
            return build_error_response(request, 502, _('SSL Error'), details=six.text_type(e))
        except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError, requests.exceptions.TooManyRedirects) as e:
            return build_error_response(request, 504, _('Connection Error'), details=six.text_type(e))

        # Build a Django response
        response = StreamingHttpResponse(res.raw.stream(4096, decode_content=False), status=res.status_code)
        if 'reason_phrase' in response:  # pragma: no cover
            # Currently only django 1.6+ supports custom reason phrases
            response.reason_phrase = res.reason_phrase

        # Add all the headers received from the response
        for header in res.headers:

            header_lower = header.lower()
            if header_lower == 'set-cookie':

                for cookie in res.cookies:
                    response.set_cookie(cookie.name, value=cookie.value, expires=cookie.expires, path=cookie.path)

            elif header_lower == 'via':

                via_header = via_header + ', ' + res.headers[header]

            elif is_valid_response_header(header_lower):
                response[header] = res.headers[header]

        # Pass proxy processors to the response
        for processor in get_response_proxy_processors():
            response = processor.process_response(request_data, response)

        response['Via'] = via_header

        return response
开发者ID:ciniguez,项目名称:FIREWA,代码行数:104,代码来源:views.py

示例7: post

# 需要导入模块: from django.http import StreamingHttpResponse [as 别名]
# 或者: from django.http.StreamingHttpResponse import set_cookie [as 别名]
    def post(self, request, *args, **kwargs):
        vol = self.object = self.get_object()

        # don't do anything if user is not logged in
        if self.request.user.is_anonymous():
            response = render(request, self.template_name, self.get_context_data())
            response.status_code = 400  # bad request
            return response

        # get posted form data and use that to generate the export
        export_form = self.get_form()
        if export_form.is_valid():
            cleaned_data = export_form.cleaned_data

            # if github export is requested, make sure user has a
            # github account available to use for access
            if cleaned_data['github']:
                try:
                    github.GithubApi.github_account(self.request.user)
                except github.GithubAccountNotFound:
                    return self.render(request, error=self.github_account_msg)

                # check that oauth token has sufficient permission
                # to do needed export steps
                gh = github.GithubApi.connect_as_user(self.request.user)
                # note: repo would also work here, but currently asking for public_repo
                if 'public_repo' not in gh.oauth_scopes():
                    return self.render(request, error=self.github_scope_msg)
        else:
            return self.render(request)

        # determine which annotations should be loaded
        if cleaned_data['annotations'] == 'user':
            # annotations *by* this user
            # (NOT all annotations the user can view)
            annotations = vol.annotations().filter(user=request.user)
        elif cleaned_data['annotations'].startswith('group:'):
            # all annotations visible to a group this user belongs to
            group_id = cleaned_data['annotations'][len('group:'):]
            # NOTE: object not found error should not occur here,
            # because only valid group ids should be valid choices
            group = AnnotationGroup.objects.get(pk=group_id)
            annotations = vol.annotations().visible_to_group(group)

        # generate annotated tei
        tei = annotate.annotated_tei(vol.generate_volume_tei(),
                                     annotations)

        # check form data to see if github repo is requested
        if cleaned_data['github']:
            try:
                repo_url, ghpages_url = export.website_gitrepo(request.user,
                    cleaned_data['github_repo'], vol, tei,
                    page_one=cleaned_data['page_one'])

                logger.info('Exported %s to GitHub repo %s for user %s',
                    vol.pid, repo_url, request.user.username)

                # NOTE: maybe use a separate template here?
                return self.render(request, repo_url=repo_url,
                    ghpages_url=ghpages_url, github_export=True)
            except export.GithubExportException as err:
                response = self.render(request, error='Export failed: %s' % err)
                response.status_code = 400  # maybe?
                return response
        else:
            # non github export: download zipfile
            try:
                webzipfile = export.website_zip(vol, tei,
                    page_one=cleaned_data['page_one'])
                logger.info('Exported %s as jekyll zipfile for user %s',
                    vol.pid, request.user.username)
                response = StreamingHttpResponse(FileWrapper(webzipfile, 8192),
                    content_type='application/zip')
                response['Content-Disposition'] = 'attachment; filename="%s_annotated_jeyll_site.zip"' % \
                    (vol.noid)
                response['Content-Length'] = os.path.getsize(webzipfile.name)
            except export.ExportException as err:
                # display error to user and redisplay the form
                response = self.render(request, error='Export failed. %s' % err)
                response.status_code = 500

            # set a cookie to indicate download is complete, that can be
            # used by javascript to hide a 'generating' indicator
            completion_cookie_name = request.POST.get('completion-cookie',
                '%s-web-export' % vol.noid)
            response.set_cookie(completion_cookie_name, 'complete', max_age=10)
            return response
开发者ID:WSULib,项目名称:readux,代码行数:90,代码来源:views.py

示例8: do_request

# 需要导入模块: from django.http import StreamingHttpResponse [as 别名]
# 或者: from django.http.StreamingHttpResponse import set_cookie [as 别名]

#.........这里部分代码省略.........
                # Only take into account request body if the request has a
                # Content-Length header (we don't support chunked requests)
                request_data['data'] = request

                # It's better not propagate the Content-Length header as
                # request processors may change final data length. In addition
                # to this, the requests modules ignores the Content-Length
                # header and tries to obtain data length directly from the
                # data parameter. Therefore, providing this value in the len
                # attribute seems to be the best option
                request_data['data'].len = int(header[1])

            elif header_name == 'cookie' or header_name == 'http_cookie':

                cookie_parser = Cookie.SimpleCookie(str(header[1]))

                del cookie_parser[settings.SESSION_COOKIE_NAME]

                if settings.CSRF_COOKIE_NAME in cookie_parser:
                    del cookie_parser[settings.CSRF_COOKIE_NAME]

                request_data['cookies'].update(cookie_parser)

            elif self.http_headerRE.match(header_name) and not header_name in self.blacklisted_http_headers:

                fixed_name = header_name.replace("http_", "", 1).replace('_', '-')
                request_data['headers'][fixed_name] = header[1]

        # Build the Via header
        protocolVersion = self.protocolRE.match(request.META['SERVER_PROTOCOL'])
        if protocolVersion is not None:
            protocolVersion = protocolVersion.group(1)
        else:
            protocolVersion = '1.1'

        via_header = "%s %s (Wirecloud-python-Proxy/1.1)" % (protocolVersion, get_current_domain(request))
        if 'via' in request_data['headers']:
            request_data['headers']['via'] += ', ' + via_header
        else:
            request_data['headers']['via'] = via_header

        # XFF headers
        if 'x-forwarded-for' in request_data['headers']:
            request_data['headers']['x-forwarded-for'] += ', ' + request.META['REMOTE_ADDR']
        else:
            request_data['headers']['x-forwarded-for'] = request.META['REMOTE_ADDR']

        request_data['headers']['x-forwarded-host'] = host
        if 'x-forwarded-server' in request_data['headers']:
            del request_data['headers']['x-forwarded-server']

        # Pass proxy processors to the new request
        try:
            for processor in get_request_proxy_processors():
                processor.process_request(request_data)
        except ValidationError as e:
            return e.get_response()

        # Cookies
        cookie_header_content = ', '.join([cookie_parser[key].OutputString() for key in request_data['cookies']])
        if cookie_header_content != '':
            request_data['headers']['Cookie'] = cookie_header_content

        # Open the request
        try:
            res = requests.request(request_data['method'], request_data['url'], headers=request_data['headers'], data=request_data['data'], stream=True)
        except requests.exceptions.HTTPError:
            return HttpResponse(status=504)
        except requests.exceptions.ConnectionError:
            return HttpResponse(status=502)

        # Build a Django response
        response = StreamingHttpResponse(res.raw.stream(4096, decode_content=False))

        # Set status code to the response
        response.status_code = res.status_code

        # Add all the headers received from the response
        for header in res.headers:

            header_lower = header.lower()
            if header_lower == 'set-cookie':

                for cookie in res.cookies:
                    response.set_cookie(cookie.name, value=cookie.value, expires=cookie.expires, path=cookie.path)

            elif header_lower == 'via':

                via_header = via_header + ', ' + res.headers[header]

            elif is_valid_response_header(header_lower):
                response[header] = res.headers[header]

        # Pass proxy processors to the response
        for processor in get_response_proxy_processors():
            response = processor.process_response(request_data, response)

        response['Via'] = via_header

        return response
开发者ID:aarranz,项目名称:wirecloud,代码行数:104,代码来源:views.py


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