■ FastAPI 클래스의 add_middleware 메소드를 사용해 HTTP 요청에 대해 HTTPS 리다이렉션을 설정하는 방법을 보여준다. ▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
import uvicorn from fastapi import FastAPI from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware fastAPI = FastAPI() fastAPI.add_middleware(HTTPSRedirectMiddleware) @fastAPI.get("/") async def getHelloWorld(): return {"message" : "Hello, World!"} if __name__ == "__main__": uvicorn.run( "main:fastAPI", host = "0.0.0.0", port = 443, reload = True, ssl_keyfile = "./certificate-private-key.pem", ssl_certfile = "./certificate.pem" ) |
▶ requirement.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
annotated-types==0.7.0 anyio==4.4.0 certifi==2024.6.2 click==8.1.7 dnspython==2.6.1 email_validator==2.1.1 exceptiongroup==1.2.1 fastapi==0.111.0 fastapi-cli==0.0.4 h11==0.14.0 httpcore==1.0.5 httptools==0.6.1 httpx==0.27.0 idna==3.7 Jinja2==3.1.4 markdown-it-py==3.0.0 MarkupSafe==2.1.5 mdurl==0.1.2 orjson==3.10.3 pydantic==2.7.3 pydantic_core==2.18.4 Pygments==2.18.0 python-dotenv==1.0.1 python-multipart==0.0.9 PyYAML==6.0.1 rich==13.7.1 shellingham==1.5.4 sniffio==1.3.1 starlette==0.37.2 typer==0.12.3 typing_extensions==4.12.1 ujson==5.10.0 uvicorn==0.30.1 uvloop==0.19.0 watchfiles==0.22.0 websockets==12.0 |
■ FastAPI 클래스의 route 메소드를 사용해 HTTP 요청에 대해 HTTPS 리다이렉션을 설정하는 방법을 보여준다. ▶ redirect_https.py
|
import uvicorn from fastapi import FastAPI from starlette.requests import Request from starlette.responses import RedirectResponse fastAPI = FastAPI() @fastAPI.route("/{_:path}") async def redirectHTTPS(request : Request): return RedirectResponse(request.url.replace(scheme = "https")) if __name__ == "__main__": uvicorn.run("redirect_https:fastAPI", host = "0.0.0.0", port = 80) |
▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
import uvicorn from subprocess import Popen from fastapi import FastAPI fastAPI = FastAPI() @fastAPI.get("/") async def getHelloWorld(): return {"message" : "Hello, World!"} if __name__ == "__main__": Popen(["python3", "-m", "redirect_https"]) uvicorn.run( "main:fastAPI", host = "0.0.0.0", port = 443, reload = True, ssl_keyfile = "./certificate-private-key.pem", ssl_certfile = "./certificate.pem" ) |
▶ requirements.py
더 읽기
■ run 함수의 ssl_keyfile/ssl_certfile 인자를 사용해 HTTPS를 설정하는 방법을 보여준다. ▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
import uvicorn from fastapi import FastAPI fastAPI = FastAPI() @fastAPI.get("/") async def getHelloWorld(): return {"message" : "Hello, World!"} if __name__ == "__main__": uvicorn.run( "main:fastAPI", host = "0.0.0.0", port = 443, reload = True, ssl_keyfile = "./certificate-private-key.pem", ssl_certfile = "./certificate.pem" ) |
▶ requirements.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
annotated-types==0.7.0 anyio==4.4.0 certifi==2024.6.2 click==8.1.7 dnspython==2.6.1 email_validator==2.1.1 exceptiongroup==1.2.1 fastapi==0.111.0 fastapi-cli==0.0.4 h11==0.14.0 httpcore==1.0.5 httptools==0.6.1 httpx==0.27.0 idna==3.7 Jinja2==3.1.4 markdown-it-py==3.0.0 MarkupSafe==2.1.5 mdurl==0.1.2 orjson==3.10.3 pydantic==2.7.3 pydantic_core==2.18.4 Pygments==2.18.0 python-dotenv==1.0.1 python-multipart==0.0.9 PyYAML==6.0.1 rich==13.7.1 shellingham==1.5.4 sniffio==1.3.1 starlette==0.37.2 typer==0.12.3 typing_extensions==4.12.1 ujson==5.10.0 uvicorn==0.30.1 uvloop==0.19.0 watchfiles==0.22.0 websockets==12.0 |
■ openssl 명령을 사용해 SSL/TLS 연결을 테스트하는 방법을 보여준다. 1. CTRL + ALT + T 키를 눌러서 [터미널]을 실행한다. 2. [터미널]에서 아래
더 읽기