LoginSignup
1
0

More than 1 year has passed since last update.

docker-compose + Raspberry Pi +nginx + Flaskで現在接続しているWi-FiのSSIDを取得するときの備忘録

Last updated at Posted at 2022-10-24

概要

docker-compose + Raspberry Pi +nginx + Flaskで、現在接続しているWi-FiのSSIDを取得しようと思った時に結構詰まったので、やり方を残しておく。

ファイル構成

.
├── docker-compose.yml
├── nginx
│   └── default.conf
└── web
    ├── app.py
    ├── Dockerfile
    ├── requirements.txt
    └── templates
        └── index.html

docker-compose.yml

ここでwpa_supplicant.confのボリュームを作成しておく!!!!!!!!!!

version: "3"
services:
  nginx:
    image: nginx:latest
    container_name: nginx
    ports:
      - "82:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    networks:
      - nginx_network

  web:
    build: ./web
    container_name: web
    ports:
      - 5002:5000
    volumes:
      - /etc/wpa_supplicant/wpa_supplicant.conf:/etc/wpa_supplicant/wpa_supplicant.conf
    networks:
      - nginx_network

networks:
  nginx_network:
    driver: bridge

default.conf

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

}

Dockerfile

# Python3.7を使用
FROM python:3.7

# 作業ディレクトリを指定
WORKDIR /app

# カレントディレクトリのファイルをDockerコンテナの「/app」 ディレクトリにコピー
ADD . /app

# インストール
RUN pip install -r requirements.txt

# 外部に公開するポートを指定
EXPOSE 5000

# コンテナの実行コマンドを指定
CMD ["python", "app.py"]

requirements.txt

Flask
sqlalchemy
flask-sqlalchemy

index.html

<!DOCTYPE html>
<html lang="jp">
<head>
    <meta charset="UTF-8">
    <title>SSIDの表示</title>
</head>
<body>
    <div>
        <table>
            <tbody>
                <p><現在の状態></p>
                <tr>
                    <li>接続中WiFiのSSID: {{wifissid}}</li>
                </tr><br>
            </tbody>
        </table>
    </div>
</body>
</html>

app.py

from flask import Flask
from flask import render_template
import re


app = Flask(__name__)

@app.route('/',methods=['POST','GET'])
def wifissid():
    reg = re.compile('(?<=")[^")]+(?=")')

    with open('/etc/wpa_supplicant/wpa_supplicant.conf', 'r',encoding='UTF-8') as f:
        lines = f.read().splitlines()
        m = re.search(reg,lines[4])
        ssid = m.group()

        return render_template('index.html', wifissid = ssid)


if __name__ == '__main__':
    app.run(host='0.0.0.0',port=5000,debug=True)

docker-composeで実行する

test@raspberrypi:~/server/flask $ docker-compose up -d
Creating network "flask_nginx_network" with driver "bridge"
Creating nginx ... done
Creating web   ... done

docker-compose でステータスを確認する

test@raspberrypi:~/server/flask $ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS         PORTS                    NAMES
15f15f4d4042   flask_web   "python app.py"          2 minutes ago    Up 2 minutes   0.0.0.0:5002->5000/tcp         web
96a20fe360ce   nginx:latest      "/docker-entrypoint.…"   59 minutes ago   Up 7 minutes   0.0.0.0:82->80/tcp       nginx

ブラウザで確認

image.png

1
0
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
0