본문 바로가기
Web/Python

FastAPI 기초(1) - 기본 개념

by SeleniumBindingProtein 2023. 1. 26.
728x90
반응형

FastAPI

  • FastAPI : API에 대한 모든 기능을 제공하는 파이썬 클래스이며, Starlette를 직접 상속하는 클래스로 Starlette의 모든 기능을 사용할 수 있음
    • Starlette : 
      • Python에서 비동기 웹 서비스를 구축하는데, 이상적인 경량 ASGI 프레임워크/툴킷
    • ASGI(Asynchronous Server Gateway Interface) :
      • 비동기 서버 게이트웨이 인터페이스로 Python 웹 어플리케이션과 웹 서버 간의 공통적인 인터페이스를 기술함. 애플리케이션 당 여러 개의 비동기 이벤트를 허용함 <-> WSGI(Web Server Gateway Interface) 
      • 특징 :
        • 함수 - async 사용, HTTP 헤더와 응답 본문을 별도의 두 가지 await send() 명령으로 보냄
          • => 다른 많은 연결의 application 및 send 호출과 동시에 교차가 가능함
      • ASGI의 작동 방식 : 
        • scope : 연결에 대한 세부사항을 가지고 있는 딕셔너리 형태
        • send : send를 비동기적으로 호출하여 애플리케이션이 클라이언트로 비동기 이벤트 메세지 보낼수 있음
        • receive : receive를 비동기적으로 호출하여 애플리케이션이 클라이언트로부터 비동기 이벤트 메세지를 받을 수 있음
    • app = FastAPI()
      • FastAPI 인스턴스 생성
      • app 변수가 FastAPI 클래스의 인스턴스가 됨
      • 모든 API를 생성하기 위한 상호작용의 주요 지점 역할
      • 명령어 실행, uvicorn 호출
        • $ uvicorn main:app --reload
    • Operation
      • HTTP 메소드
        • POST - app.post("/")
        • GET - app.get("/")
        • PUT - app.put("/")
        • DELETE - app.delete("/")
          • / -> 경로
          • post, get, put, delete -> 동작
          • async def root(): -> 함수
          • return -> dict, list, str, int 등으로 반환할 수 있음
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}

 

FastAPI 공식 문서

https://fastapi.tiangolo.com/

 

FastAPI

FastAPI FastAPI framework, high performance, easy to learn, fast to code, ready for production Documentation: https://fastapi.tiangolo.com Source Code: https://github.com/tiangolo/fastapi FastAPI is a modern, fast (high-performance), web framework for buil

fastapi.tiangolo.com

 

728x90
반응형

'Web > Python' 카테고리의 다른 글

FastAPI 기초(4) - 쿼리 매개변수  (0) 2023.01.30
FastAPI 기초(3) - 경로 매개변수  (0) 2023.01.30
FastAPI 기초(2) - Python coding style  (1) 2023.01.26

댓글