如何修改 blobstoreuploadhandler 以承认状态 200?

发布于 2024-12-02 03:21:33 字数 1843 浏览 1 评论 0原文

据说 GAE SDK 1.5.4 已经删除了 blobstoreuploadhandler 必须返回重定向的要求,并且在生产中据说已经这样了,以便处理程序可以使用模板变量对模板做出“常规”响应。我需要 dev_appserver 具有此功能,因此询问如何修改 dev_appserver 以允许处理程序使用模板变量呈现模板。我想我需要更改的代码在文件中 http://code.google .com/p/googleappengine/source/browse/trunk/python/google/appengine/tools/dev_appserver_blobstore.py 但我不确定要修改什么。您能告诉我如何使处理程序能够定期响应吗?

  def EndRedirect(self, redirected_outfile, original_outfile):
      """Handle the end of upload complete notification.

      Makes sure the application upload handler returned an appropriate status
      code.
      """
      response = dev_appserver.RewriteResponse(redirected_outfile)
      logging.info('Upload handler returned %d', response.status_code)

      if (response.status_code in (301, 302, 303) and
          (not response.body or len(response.body.read()) == 0)):
        contentless_outfile = cStringIO.StringIO()


        contentless_outfile.write('Status: %s\n' % response.status_code)
        contentless_outfile.write(''.join(response.headers.headers))
        contentless_outfile.seek(0)
        dev_appserver.URLDispatcher.EndRedirect(self,
                                                contentless_outfile,
                                                original_outfile)
      else:
        logging.error(
            'Invalid upload handler response. Only 301, 302 and 303 '
            'statuses are permitted and it may not have a content body.')
        original_outfile.write('Status: 500\n\n')

更新:解决方案已发布在此链接 https:// groups.google.com/forum/#!topic/google-appengine-python/vnvhUG1-UN0

It's said that GAE SDK 1.5.4 has removed the requirement that blobstoreuploadhandler must return a redirect and on production it's supposedly already so that the handler can make a "regular" response to a template with template variables. I need this capability with dev_appserver and therefore ask how I can modify the dev_appserver to admit the handler to render a template with template variables. I suppose that code I need to change is in the file
http://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/tools/dev_appserver_blobstore.py
but I'm not sure what to modify. Can you tell me how to make the handler capable of a regular response?

  def EndRedirect(self, redirected_outfile, original_outfile):
      """Handle the end of upload complete notification.

      Makes sure the application upload handler returned an appropriate status
      code.
      """
      response = dev_appserver.RewriteResponse(redirected_outfile)
      logging.info('Upload handler returned %d', response.status_code)

      if (response.status_code in (301, 302, 303) and
          (not response.body or len(response.body.read()) == 0)):
        contentless_outfile = cStringIO.StringIO()


        contentless_outfile.write('Status: %s\n' % response.status_code)
        contentless_outfile.write(''.join(response.headers.headers))
        contentless_outfile.seek(0)
        dev_appserver.URLDispatcher.EndRedirect(self,
                                                contentless_outfile,
                                                original_outfile)
      else:
        logging.error(
            'Invalid upload handler response. Only 301, 302 and 303 '
            'statuses are permitted and it may not have a content body.')
        original_outfile.write('Status: 500\n\n')

Update: The solution was posted at this link https://groups.google.com/forum/#!topic/google-appengine-python/vnvhUG1-UN0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2024-12-09 03:21:33

解决方案已发布,我可以验证这是否有效:

def EndRedirect(self, dispatched_output, original_output):

"""Handle the end of upload complete notification. Makes sure the application upload handler returned an appropriate status code. """

    response = dev_appserver.RewriteResponse(dispatched_output)

    logging.info('Upload handler returned %d', response.status_code)
    outfile = cStringIO.StringIO()
    outfile.write('Status: %s\n' % response.status_code)

    if response.body and len(response.body.read()) > 0:
      response.body.seek(0)
      outfile.write(response.body.read())
    else:
      outfile.write(''.join(response.headers.headers))

    outfile.seek(0)

dev_appserver.URLDispatcher.EndRedirect(self,outfile,original_output)

The solution was posted and I could verify that this worked:

def EndRedirect(self, dispatched_output, original_output):

"""Handle the end of upload complete notification. Makes sure the application upload handler returned an appropriate status code. """

    response = dev_appserver.RewriteResponse(dispatched_output)

    logging.info('Upload handler returned %d', response.status_code)
    outfile = cStringIO.StringIO()
    outfile.write('Status: %s\n' % response.status_code)

    if response.body and len(response.body.read()) > 0:
      response.body.seek(0)
      outfile.write(response.body.read())
    else:
      outfile.write(''.join(response.headers.headers))

    outfile.seek(0)

dev_appserver.URLDispatcher.EndRedirect(self,outfile,original_output)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文