• 회원가입
  • 로그인
  • 구글아이디로 로그인

FastAPI - 문법 핵심정리

목차
  1. FastAPI 설치하기
  2. /main.py 파일 생성
  3. 서버 실행 (= /main.py 파일의 app 객체 실행)
  4. URL 주소 접속해 웹페이지 확인

 

※ 설치 환경: Python 3.8+ / 웹 부분: Starlette / 데이터 부분: Pydantic

 

FastAPI 설치하기

(리눅스 shell / 윈도우 cmd ) 모드에서 아래 명령어 실행.

 

# FastAPI 설치

pip install fastapi

 

# 프로덕션을 위해 Uvicorn (또는 Hypercorn)과 같은 ASGI 서버도 필요

pip install "uvicorn[standard]"

 

 

/main.py 파일 생성

 

from typing import Union

from fastapi import FastAPI


app = FastAPI()


@app.get("/")

def read_root():

    return {"홈짱닷컴": "Homzzang.com"}


@app.get("/items/{item_id}")

def read_item(item_id: int, q: Union[str, None] = None):

    return {"item_id": item_id, "q": q}

 

PS. 회원 님 코드가 async/await 사용한다면, async def 사용.

 

서버 실행 (= /main.py 파일의 app 객체 실행)

# (리눅스 Shell / 윈도우 cmd)에서 아래 명령어 실행.

 

uvicorn main:app --reload


 

URL 주소 접속해 웹페이지 확인

※ uvicorn 경우, 기본적으로 8000 포트 사용. 

 


 


분류 제목
Tutorial FastAPI - 폼 데이터 (Form Data)
Tutorial FastAPI - 파일 요청 (Request Files)
Tutorial FastAPI - 폼 및 파일 요청 (Request Forms and Files)
Tutorial FastAPI - 에러 처리 (Handling Errors)
Tutorial FastAPI - 경로 작동 설정 (Path Operation Configuration)
Tutorial FastAPI - JSON 호환 가능 인코더 (JSON Compatible Encoder)
Tutorial FastAPI - 본문 - 업데이트 (Body - Updates)
Tutorial FastAPI - 의존성 (Dependencies)
Tutorial FastAPI - 보안 (Security)
Tutorial FastAPI - 미들웨어 (Middleware)
Tutorial FastAPI - 교차 출처 리소스 공유 (CORS: Cross-Origin Resource Sharing)
Tutorial FastAPI - SQL 관계 데이터베이스 (SQL Relational Databases)
Tutorial FastAPI - 더 큰 애플리케이션 - 여러 파일들 (Bigger Applications - Multipl…
Tutorial FastAPI - 백그라운드 작업 (Background Tasks)
Tutorial FastAPI - 메타데이터 & 문서주소 (Metadata and Docs URLs)
Tutorial FastAPI - 정적 파일 (Static Files)
Tutorial FastAPI - 테스트 (= 테스팅 Testing)
Tutorial FastAPI - 버그 제거 (= 디버깅 (Debugging)
Advanced FastAPI - 경로 작업 고급 구성 (Path Operation Advanced Configuration…
Advanced FastAPI - 추가 상태 코드 (Additional Status Codes)
2/5
목록
찾아주셔서 감사합니다. Since 2012