【问题标题】:Python webapp2 project structure configurationPython webapp2项目结构配置
【发布时间】:2017-10-06 18:47:21
【问题描述】:

我想像下一个结构一样组织我的项目,但是当我尝试对其进行测试时遇到问题

在 handlers 文件夹中,我有一个名为 Base.py 的文件和一个类:

 def get_success_reponse(**kwargs):
     kwargs.update(
       dict(
        status="SUCCESS",
       )
     )
     return kwargs

 class BaseHandler(webapp2.RequestHandler):
       property = 0

在同一个文件夹处理程序中,我有另一个文件名为:EchoHandler.py 和一个类

import Base

class EchoHandler(Base.BaseHandler):
    def post(self): 
        logger.info("test")       
        data = json.loads(self.request.body)
        echo = data.get("echo")
    return self.json_data(get_success_reponse(echo))

我的 main.py 文件看起来像

 import webapp2
 import config

 app = webapp2.WSGIApplication([
      webapp2.Route('/x/v1/echo', handler='handlers.EchoHandler')
 ], debug=True, config=config.WEBAPP2CONFIG)

我的 app.yaml

runtime: python27
api_version: 1
threadsafe: false

handlers:
- url: /x/.*
  script: main.py  

libraries:
- name: webapp2
  version: latest
- name: jinja2
  version: latest
- name: ssl
  version: latest

问题

当我向http://localhost:8080/x/v1/echo 发出 POST 请求时,发送此数据:

  {
    "echo": "Test"
  }

我收到“200 OK”响应,但没有收到任何 json 响应,没有写入日志

如果我将这个“http://localhost:8080/x/v1/echo”更改为“http://localhost:8080/x/v1/echoasdfa”,我也会收到 200。

你能帮帮我吗?

【问题讨论】:

    标签: python python-2.7 webapp2 google-app-engine-python


    【解决方案1】:

    这是一个很老的问题,但无论如何我都会回答它。确保在返回之前将数据写入处理程序中的 Response 对象。您没有提供 self.json_data() 方法的代码,但我假设您没有将 json 数据写入 Response 对象,即使您正在返回它。此外,这可能只是问题中的错字,但看起来您的 return self.json_data(get_success_reponse(echo)) 行没有缩进。它应该与 post() 代码的其余部分处于相同的缩进级别。

    无论如何,试试这个:

    import Base
    import json
    
    class EchoHandler(Base.BaseHandler):
        def post(self): 
            logger.info("test")       
            data = json.loads(self.request.body)
            echo = data.get("echo")
    
            # get the dict you want to convert to JSON
            echo_dict = get_success_response(echo)
    
            # convert it to a JSON string
            echo_json = json.dumps(echo_dict)
    
            # set the content type of the Response to JSON
            self.response.content_type = 'application/json'
    
            # write the JSON to the Response body and return the Response
            return self.response.write(echo_json)
    

    您需要将数据写入响应以便将该数据发送到客户端。在写入响应对象后,您不需要需要返回响应对象,但我还是喜欢这样做,因为我认为它看起来更清晰。

    另外,您的路线看起来一团糟。尝试将您的 main.py 更改为:

     import webapp2
     import config
    
     # changed the lazy import line (handler='handlers.EchoHandler.EchoHandler')
     app = webapp2.WSGIApplication([
          webapp2.Route('/x/v1/echo', handler='handlers.EchoHandler.EchoHandler')
     ], debug=True, config=config.WEBAPP2CONFIG)
    

    或者,我的做法是将处理程序实际导入 main.py:

     import webapp2
     import config
     from handlers import EchoHandler
    
     # note: no quotes around EchoHandler.EchoHandler
     app = webapp2.WSGIApplication([
          webapp2.Route('/x/v1/echo', handler=EchoHandler.EchoHandler)
     ], debug=True, config=config.WEBAPP2CONFIG)
    

    【讨论】:

      猜你喜欢
      • 2013-08-22
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 2017-12-26
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多