====== Язык программирования Python ======
===== Виртуальная среда 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://www.workroom-productions.com/serving-a-directory-with-flask/|Publishing a Directory with Flask]]
# apt install python3-venv
$ python3 -m venv ~/venv1
$ source ~/venv1/bin/activate
(venv1) $ which python
(venv1) $ which ansible
(venv1) $ deactivate
===== Web приложение =====
* [[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://docs.python.org/3/library/configparser.html|configparser — Configuration file parser]]
* [[#Виртуальная среда Python]]
(venv1) :~$ pip install Flask
(venv1) :~$ mkdir -p pywebd/; cd $_
(venv1) :~/pywebd$ cat app.py
from flask import Flask, send_from_directory
import os
import configparser
app = Flask(__name__)
@app.route('/')
def index():
return send_from_directory(pywebd_doc_root, 'index.html')
@app.route('/')
def sendstuff(path):
print(path)
return send_from_directory(pywebd_doc_root, path)
if __name__ == "__main__":
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)
# mkdir -p /etc/pywebd/
# cat /etc/pywebd/pywebd.conf
[default]
DocumentRoot = /var/www/
Listen = 4080
# #cp /root/wild.crt /etc/pywebd/pywebd.crt
# #cp /root/wild.key /etc/pywebd/pywebd.key
* [[Средства программирования shell#Ресурсы Web сервера на shell]]
(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$ pip freeze | tee requirements.txt
===== CRUD Rest API приложение =====
* [[https://dev.to/francescoxx/python-crud-rest-api-using-flask-sqlalchemy-postgres-docker-docker-compose-3kh4|Python CRUD Rest API using Flask, SQLAlchemy, Postgres, Docker, Docker Compose]]
===== Zabbix API приложение =====
* [[https://www.zabbix.com/documentation/current/en/manual/api]]
* [[https://sbcode.net/zabbix/zabbix-api-python-example/]]
* [[https://forum.checkmk.com/t/rest-api-python-question-how-to-use-variables-in-json-post-in-key-and-value/34652]]
(venv1) server:~# pip install requests
(venv1) server:~# cat zab_get_problem.py
import requests
import json
ZABBIX_API_URL = "http://127.0.0.1/zabbix/api_jsonrpc.php"
UNAME = "Admin"
PWORD = "zabbix"
r = requests.post(ZABBIX_API_URL,
json={
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"username": UNAME,
"password": PWORD},
"id": 1
})
print(json.dumps(r.json(), indent=4, sort_keys=True))
AUTHTOKEN = r.json()["result"]
print(AUTHTOKEN)
# Retrieve a list of problems
print("\nRetrieve a list of problems")
r = requests.post(ZABBIX_API_URL,
headers={'Authorization': 'Bearer ' + AUTHTOKEN},
json={
"jsonrpc": "2.0",
"method": "problem.get",
"params": {},
"id": 2,
})
print(json.dumps(r.json(), indent=4, sort_keys=True))
#Logout user
print("\nLogout user")
r = requests.post(ZABBIX_API_URL,
headers={'Authorization': 'Bearer ' + AUTHTOKEN},
json={
"jsonrpc": "2.0",
"method": "user.logout",
"params": {},
"id": 2,
})
print(json.dumps(r.json(), indent=4, sort_keys=True))
(venv1) server:~# python3 zab_get_problem.py
===== Дополнительные материалы =====
==== Доступ к каталогу по http ====
* [[https://gist.github.com/SeanPesce/af5f6b7665305b4c45941634ff725b7a|SeanPesce/https_server.py]]
dir$ python3 -m http.server 80