1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

RasPi+FastAPIを使う

Last updated at Posted at 2021-01-10

ラズベリーパイ上でFastAPIを使用するために、Python3.8以上をインストールする必要があるようなので、まずPythonのインストールを行い、そのあとでFastAPIのインストールを行う。

FastAPIをインストールするために、ラズベリーパイのPythonを3.8.xへアップデートする。

$ sudo apt-get install -y build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev tar wget vim

$ wget https://www.python.org/ftp/python/3.8.7/Python-3.8.7.tgz
$ sudo tar zxf Python-3.8.7.tgz
$ cd Python-3.8.7
$ sudo ./configure --enable-optimizations
$ sudo make -j 4
$ sudo make altinstall
$ echo "alias python=/usr/local/bin/python3.8" >> ~/.bashrc
$ echo "alias pip=/usr/local/bin/pip3.8" >> ~/.bashrc
$ source ~/.bashrc

FastAPIをインストール

$ pip install pip --upgrade
$ pip install fastapi
$ pip install uvicorn

Python サーバサンプル

dht_server.py
import uvicorn
from fastapi import FastAPI

app = FastAPI()

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

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

サーバ起動

 $ python dht_server.py 
INFO:     Started server process [10939]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:40366 - "GET / HTTP/1.1" 200 OK
INFO:     127.0.0.1:40370 - "GET /favicon.ico HTTP/1.1" 404 Not Found
1
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?