feat: Add initial client and server templates for FastAPI tutorial.

This commit is contained in:
2026-02-28 12:45:54 +09:00
parent 0966bd9a61
commit 8c0819feea
2 changed files with 85 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
import requests
import sys
SERVER_URL = "http://localhost:8000"
def post_message(user_name, content):
def get_messages(start_index):
"""서버로부터 새로운 메시지를 가져옵니다."""
try:
response = requests.get(f"{SERVER_URL}/messages", params={"start": start_index})
if response.status_code == 200:
new_messages = response.json()
if not new_messages:
print("새로운 메시지가 없습니다.")
else:
for msg in new_messages:
print(f"[{msg['timestamp']}] {msg['name']}: {msg['message']}")
return new_messages
else:
print(f"메시지 수신 실패: {response.text}")
return []
except Exception as e:
print(f"오류 발생: {e}")
return []
def main():
print("FastAPI 채팅 클라이언트에 오신 것을 환영합니다!")
user_name = input("이름을 입력해주세요 > ").strip()
if not user_name:
user_name = "Anonymous"
start_index = 0
while True:
print("\n수행할 작업을 선택해주세요:")
print("1. 메시지 전송")
print("2. 수신한 메시지 보기")
print("3. 종료")
choice = input("> ").strip()
if choice == "1":
elif choice == "2":
elif choice == "3":
else:
print("잘못된 입력입니다. 1~3 사이의 숫자를 입력해주세요.")
if __name__ == "__main__":
main()
+26
View File
@@ -0,0 +1,26 @@
from fastapi import FastAPI, Query
from pydantic import BaseModel
from datetime import datetime
from typing import List
app = FastAPI()
# 메시지 저장을 위한 리스트
messages = []
class Message(BaseModel):
name: str
message: str
@app.post("/messages")
def post_message(msg: Message):
@app.get("/messages")
def get_messages(start: int = 0):
if __name__ == "__main__":
import uvicorn
# 터미널에서 직접 실행할 때를 위해 uvicorn 설정 추가
uvicorn.run(app, host="0.0.0.0", port=8000)