2017-05-08 5 views
0
나는 다음 구조처럼 내 프로젝트를 구성하고 싶습니다

하지만 webapp2 프로젝트 구조 구성 : Base.py 내부에 하나 개의 클래스 :파이썬 내가 핸들러는 I라는 이름의 파일이 폴더에 그것을</p> <p><a href="https://i.stack.imgur.com/hxfOm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hxfOm.png" alt="enter image description here"></a></p> <p>을 테스트하려고 할 때이 문제가

,727 내부 EchoHandler.py와 하나 개의 클래스 : 같은 폴더 처리기에서

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

class BaseHandler(webapp2.RequestHandler): 
     property = 0 

나는 다른라는 이름의 파일이

내 main.py 파일

import webapp2 
import config 

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

내 애플리케이션 제목을

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 요청 sendind이 데이터 작업을 수행 할 때 :

{ 
    "echo": "Test" 
    } 

나는 응답 "200 OK"를 수신하지만 난이 "http://localhost:8080/x/v1/echo"로 변경 "http://localhost:8080/x/v1/echoasdfa"나는 너무 좋아 (200)를 수신 할 경우 나는 어떤 로그가

를 기록되지 않습니다 어떤 JSON 응답을하지 않습니다.

도와 주시겠습니까?

답변

0

이것은 꽤 오래된 질문이지만 어쨌든 대답하겠습니다. 반환하기 전에 처리기의 응답 개체에 데이터를 쓰고 있는지 확인하십시오. self.json_data() 메서드에 대한 코드를 제공하지 않았지만 반환하는 경우에도 응답 개체에 json 데이터를 쓰지 않는 것으로 가정합니다. 또한이 질문에 오타가있을 수 있지만 귀하의 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 
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) 
: 내가 할 것입니다 방법을

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에 핸들러를 가져 오는 것입니다