Flask Setting, VSCode, Debugging

김와티·2022년 12월 20일
0

설정, 간단히 정리

  • python 설치

  • flask 설치

    pip install -U flask
  • vscode 설치

  • virtualenv 설치 (venv이 경량화 버전, 그러므로 virtualenv사용)

    • 서버 리소스 걱정이 든다면 venv
    pip install -U virtualenv
  • flask 서버 구동

    • GET, POST 방식 확인
      # save this as app.py
      from flask import Flask, make_response
      from flask import request
    
      app = Flask(__name__)
    
      user = "newUser"
      password = "password"
      headers = {'content-type', 'application/json'}
    
      @app.route("/", methods=['GET','POST'])
      def index():
          print(request.method)
          if request.method == 'GET':
              # query string 가져오는거
              name = request.args.get('name')
              print(f'request: {request}')
              return f'Index Page, name:{name}'
          elif request.method == 'POST':
              json = request.json
              key1 = request.form.get("key1")
              print(f'결과: {key1}')
              # print(f'request: {request["number"]}')
              return f"\n{request.form}\n{request.json}"
    
      @app.route("/hello")
      def hello():
          return "Hello, World!"
    
      if __name__ == '__main__':
      #   app.run(debug=True, host='0.0.0.0', port=3000)
        app.run(host='0.0.0.0', port=3000)

curl 명령어

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000

vscode, flask 디버깅 모드 설정

  • interpretervirtualenv 사용
  • "justMyCode":false
  • launch.json 설정
    {
      // Use IntelliSense to learn about possible attributes.
      // Hover to view descriptions of existing attributes.
      // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
      "version": "0.2.0",
      "configurations": [
          {
              "name": "Python: Flask",
              "type": "python",
              "request": "launch",
              "module": "flask",
              "env": {
                  "FLASK_APP": "app.py",
                  "FLASK_DEBUG": "1"
              },
              "args": [
                  "run",
                  "--no-debugger",
                  "--no-reload"
              ],
              "jinja": true,
              "justMyCode": false
          }
      ]
    }

400 Bad request. The browser (or proxy) sent a request that this server could not understand.
파라미터 json 데이터 확인 이후 조작하기

profile
딸 바보, SYOON 사랑해, RPA 운영/개발 공유하고자 하는 사람. RPA와 연계 필요한 대상은 가리지 않고 습득한다는 마음으로

0개의 댓글