This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
язык_программирования_python [2024/12/02 09:11] val [Web приложение] |
язык_программирования_python [2025/03/16 11:44] (current) val [Web приложение] |
||
---|---|---|---|
Line 4: | Line 4: | ||
* [[https://blog.sedicomm.com/2021/06/29/chto-takoe-venv-i-virtualenv-v-python-i-kak-ih-ispolzovat/|Что такое venv и virtualenv в Python, и как их использовать]] | * [[https://blog.sedicomm.com/2021/06/29/chto-takoe-venv-i-virtualenv-v-python-i-kak-ih-ispolzovat/|Что такое venv и virtualenv в Python, и как их использовать]] | ||
* [[https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/|https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/]] | * [[https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/|https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/]] | ||
+ | * [[https://www.workroom-productions.com/serving-a-directory-with-flask/|Publishing a Directory with Flask]] | ||
<code> | <code> | ||
Line 22: | Line 23: | ||
* [[https://python.ivan-shamaev.ru/run-install-deploy-flask-web-app-docker-dockerfile-compose/|Создание Web-приложения Flask и деплой с помощью Docker Compose & Dockerfile]] | * [[https://python.ivan-shamaev.ru/run-install-deploy-flask-web-app-docker-dockerfile-compose/|Создание Web-приложения Flask и деплой с помощью Docker Compose & Dockerfile]] | ||
* [[https://blog.miguelgrinberg.com/post/running-your-flask-application-over-https|Running Your Flask Application Over HTTPS]] | * [[https://blog.miguelgrinberg.com/post/running-your-flask-application-over-https|Running Your Flask Application Over HTTPS]] | ||
+ | * [[https://docs.python.org/3/library/configparser.html|configparser — Configuration file parser]] | ||
* [[#Виртуальная среда Python]] | * [[#Виртуальная среда Python]] | ||
Line 32: | Line 34: | ||
(venv1) :~/pywebd$ cat app.py | (venv1) :~/pywebd$ cat app.py | ||
</code><code> | </code><code> | ||
- | from flask import Flask | + | from flask import Flask, send_from_directory |
- | + | import os | |
- | CONST_VER = "ver1.1" | + | import configparser |
- | + | ||
- | import socket | + | |
- | hostname = socket.gethostname() | + | |
app = Flask(__name__) | app = Flask(__name__) | ||
@app.route('/') | @app.route('/') | ||
- | def home(): | + | def index(): |
- | return '<h3>Hello world from pywebd on host ' + hostname + ' ' + CONST_VER + '</h3>' | + | return send_from_directory(pywebd_doc_root, 'index.html') |
+ | |||
+ | @app.route('/<path:path>') | ||
+ | def sendstuff(path): | ||
+ | print(path) | ||
+ | return send_from_directory(pywebd_doc_root, path) | ||
if __name__ == "__main__": | if __name__ == "__main__": | ||
- | app.run(debug=True, host='0.0.0.0', port=8080) | + | config = configparser.ConfigParser() |
+ | config.read('/etc/pywebd/pywebd.conf') | ||
+ | # pywebd_port = os.environ.get('PYWEBD_PORT',config['default']['Listen']) | ||
+ | # pywebd_doc_root = os.environ.get('PYWEBD_DOC_ROOT',config['default']['DocumentRoot']) | ||
+ | if 'PYWEBD_PORT' in os.environ: | ||
+ | pywebd_port = os.environ.get('PYWEBD_PORT') | ||
+ | else: | ||
+ | pywebd_port = config['default']['Listen'] | ||
+ | if 'PYWEBD_DOC_ROOT' in os.environ: | ||
+ | pywebd_doc_root = os.environ.get('PYWEBD_DOC_ROOT') | ||
+ | else: | ||
+ | pywebd_doc_root = config['default']['DocumentRoot'] | ||
+ | |||
+ | app.run(host="0.0.0.0", port=pywebd_port, debug=True) | ||
+ | # app.run(ssl_context=('/etc/pywebd/pywebd.crt', '/etc/pywebd/pywebd.key'), debug=True, host='0.0.0.0', port=pywebd_port) | ||
</code><code> | </code><code> | ||
+ | # mkdir -p /etc/pywebd/ | ||
+ | |||
+ | # cat /etc/pywebd/pywebd.conf | ||
+ | </code><code> | ||
+ | [default] | ||
+ | DocumentRoot = /var/www/ | ||
+ | Listen = 4080 | ||
+ | </code><code> | ||
+ | # #cp /root/wild.crt /etc/pywebd/pywebd.crt | ||
+ | # #cp /root/wild.key /etc/pywebd/pywebd.key | ||
+ | </code> | ||
+ | |||
+ | * [[Средства программирования shell#Ресурсы Web сервера на shell]] | ||
+ | |||
+ | <code> | ||
+ | (venv1) :~/pywebd$ #export PYWEBD_PORT=4443 | ||
+ | (venv1) :~/pywebd$ #export PYWEBD_DOC_ROOT=/var/www/html/ | ||
+ | (venv1) :~/pywebd$ #PYWEBD_PORT=4443 PYWEBD_DOC_ROOT=/var/www/html/ python app.py | ||
+ | |||
(venv1) :~/pywebd$ python app.py | (venv1) :~/pywebd$ python app.py | ||
- | (venv1) :~/pywebd$ pip freeze > requirements.txt | + | (venv1) :~/pywebd$ pip freeze | tee requirements.txt |
</code> | </code> | ||