environment
Raspi 3B+
OS rasbian (stretch)
pip 20.2.4
python 3.5
npm 6.14.9
forever v3.0.2
flask 0.12.1
Various installations
$ pip install Flask
$ npm install -g forever
(Reference) https://qiita.com/tomboyboy/items/122dfdb41188176e45b5
hoge.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello world'
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=5000)
Run(test)
$ python hoge.py
^C
Permittion Deny
, so use another one.
(Reference) https://stackoverflow.com/questions/550032/what-causes-python-socket-errorcurl 192.168.x.x: 5000
passes from inside Raspberry Pi but cannot be accessed from outside)
$ sudo ufw allow 5000
implemented.If this is left as it is, the process will end when exiting ssh, so make it persistent with forever. For forever, the command is node by default, and you can specify any command by using the -c
option, and you can also make Python code persistent.
Perform persistence
$ forever start -c python hoge.py
See below for forever commands and options https://qiita.com/disc99/items/57490f5eef3e2eb685ba
Even with the above, if you reboot the Raspberry Pi itself, forever will not be executed at startup, so the server is dead.
Write the following in /etc/rc.local
(Reference) https://584homes.com/it/raspberry-pi/rasberrypi-startup1803.html%20=
rc.local
sudo forever start -a --uid Hoge -c python /home/pi/hoge.py
exit 0
Recommended Posts