기본 콘텐츠로 건너뛰기

Flask Request Handler, Error Handler

Flask Request Handler, Error Handler

Python/Flask

Request Handler

""" before_first_request : 웹 어플리케이션 기동 이후 가장 처음에 들어오는 HTTP 요청에서만 실행 before_request : 매 요청시 실행 after_request : 요청이 끝나 브라우저에 응답하기 전에 실행 teardown_request : 요청의 결과가 브라우저에 응답한 다음 실행 teardown_appcontext : HTTP 요청이 완료 되면 실행 되며, 애플리케이션 컨텍스트 내에서 실행 """ # 웹 브라우저로부터 HTTP 요청이 들어올 때마다 호출되는 메소드 @app.before_request def before_request(): g.temp = 'before_request' # print(getattr(g, 'temp', None)) # 요청의 처리가 완료될 때 호출되는 메소드 @app.teardown_request def teardown_request(exception): g.temp = 'teardown_request' db.session.remove() # print(getattr(g, 'temp', None))

Error Handler

# 에러 핸들러 및 라우팅 처리를 해줌 @app.errorhandler(HTTPException) def error_handler(e): response = e.get_response() response.data = json.dumps({ 'code': e.code, 'msg' : e.name, 'desc': e.description, }) response.content_type = 'application/json' return response

from http://sanggi-jayg.tistory.com/40 by ccl(A) rewrite - 2020-03-07 07:54:42

댓글

이 블로그의 인기 게시물

Flask 13. pythonanywhere에 배포하기

Flask 13. pythonanywhere에 배포하기 Login: PythonAnywhere It's always a pleasure to hear from you! Ask us a question, or tell us what you love or hate about PythonAnywhere. We'll get back to you over email ASAP. Sorry, there was an error connecting to the server. Please try again in a few moments... www.pythonanywhere.com from http://ohdowon064.tistory.com/124 by ccl(A) rewrite - 2020-03-11 15:54:10

[ubuntu] FLASK_APP

[ubuntu] FLASK_APP Development/Debugging 🐞 FLASK_ENV=development FLASK_APP = app.py flask run zsh: command not found: FLASK_APP ✔️ FLASK_ENV=development FLASK_APP=app.py flask run 띄어쓰기를 해서 저런 오류를 출력할수도 있구나 😲 참고 : 108p에서 FLASK가 FKAS로 오타나있다. from http://hee-stories.tistory.com/18 by ccl(A) rewrite - 2020-03-24 17:20:11