uvicornとFastAPIによるサーバー構築 その1

server.py

import uvicorn
from fastapi import FastAPI


app = FastAPI()


@app.post("/hoge_post")
async def hoge_func():
print('hoge')


def start():
config = uvicorn.Config("server:app", host="127.0.0.1", port=8000, log_level="info")
server = uvicorn.Server(config=config)
server.run()


if __name__ == '__main__':
start()

 

cliant.py

import json
import urllib.request


def client():
req = urllib.request.Request(
'http://127.0.0.1:8000/hoge_post',
headers={'Content-Type': 'application/json'},
data=json.dumps({}).encode('utf-8')
)
urllib.request.urlopen(req)


if __name__ == '__main__':
client()